Skip to content

Commit

Permalink
Add property trait, refactor category utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest committed Jun 3, 2024
1 parent 2bb5a36 commit 8dd4d83
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- This option will prevent the fixtures from being executed but still prints all fixtures it would execute
- Added new DatabaseUtils with a few helpful methods:
- `deleteEntities` takes an entity name and criteria and deletes all entities which match the criteria
- Added a small cache for all utilities. It prevents loading data twice within the same request / command execution

### Changed
- Changed argument type on `SalesChannelUtils::getTax()` from `int` to `float`
Expand All @@ -28,6 +29,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Dropped support for PHP 8.1
- Dropped support for Shopware 6.3 & 6.4
- Removed FixtureBag
- CategoryUtils
- Removed method `getFirst` on CategoryUtils
- Removed method `getByName` on CategoryUtils

## [2.4.0] - 2023-11-15
### Added
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"php": "^8.2 || ^8.3",
"shopware/core": "6.5.*|6.6.*",
"shopware/administration": "6.5.*|6.6.*",
"shopware/storefront": "6.5.*|6.6.*"
"shopware/storefront": "6.5.*|6.6.*",
"spatie/once": "^3.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.57.2",
Expand Down
6 changes: 3 additions & 3 deletions src/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

abstract class Fixture
{
protected readonly FixtureHelper $helper;
protected FixtureHelper $helper;

abstract public function load(): void;

Expand All @@ -28,9 +28,9 @@ public function groups(): array
}

/**
* @internal This method should only be called from the FixtureLoader.
* @internal this method should only be called from the FixtureLoader
*/
public final function setHelper(FixtureHelper $helper): void
final public function setHelper(FixtureHelper $helper): void
{
$this->helper = $helper;
}
Expand Down
3 changes: 1 addition & 2 deletions src/FixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class FixtureLoader
public function __construct(
\Traversable $fixtures,
private readonly FixtureHelper $helper,
)
{
) {
$this->fixtures = iterator_to_array($fixtures);
}

Expand Down
86 changes: 86 additions & 0 deletions src/Traits/PropertyFixtureTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Basecom\FixturePlugin\Traits;

use Basecom\FixturePlugin\Fixture;

/**
* This trait provides helper methods for fixtures to create property groups and options.
* It provides typed methods to give all relevant data, while maintaining the ability to extend
* this with additional data.
*
* Notice: This trait is designed to only work with the Fixture class. If you want to use it elsewhere,
* you need to make sure the FixtureHelper is accessible as `$this->helper`.
*
* @mixin Fixture
*/
trait PropertyFixtureTrait
{
/**
* @param array<mixed> $options
* @param array<string, mixed> $customFields
* @param array<string, mixed> $rawData
*
* @return array<string, mixed>
*/
protected function createPropertyGroupData(
string $id,
string $englishName,
string $germanName,
?array $options = null,
?array $customFields = null,
array $rawData = [],
): array {
// $translations = [];
// foreach(array_keys($names) as $locale) {
// $translations[$locale] = ['name' => $names[$locale]];
// }

$data = [
'id' => $id,
'translations' => [
'de-DE' => [
'name' => $germanName,
],
'en-GB' => [
'name' => $englishName,
],
],
'options' => $options,
'customFields' => $customFields,
];

return array_merge_recursive($data, $rawData);
}

/**
* @return array<string, mixed>
*/
protected function createPropertyOptionData(
string $id,
string $englishName,
string $germanName,
?string $colorHexCode = null,
?string $coverUrl = null,
?string $coverAlt = null,
): array {
return [
'id' => $id,
'translations' => [
'de-DE' => [
'name' => $germanName,
],
'en-GB' => [
'name' => $englishName,
],
],
'customFields' => [
'property_group_option_cover_url' => $coverUrl,
'property_group_option_cover_alt' => $coverAlt,
],
'colorHexCode' => $colorHexCode,
];
}
}
61 changes: 23 additions & 38 deletions src/Utils/CategoryUtils.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/** @noinspection ALL */

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;
Expand All @@ -13,6 +11,15 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

/**
* This class provides utility methods to work with categories. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Category()->……();
* ```
*/
readonly class CategoryUtils
{
/**
Expand All @@ -23,46 +30,24 @@ public function __construct(
) {
}

public function getRootCategory(): ?CategoryEntity
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('autoIncrement', 1))
->addFilter(new EqualsFilter('level', 1))
->setLimit(1);

$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();

return $category instanceof CategoryEntity ? $category : null;
}

public function getFirst(): ?CategoryEntity
{
$criteria = (new Criteria())->addFilter(
new EqualsFilter('level', '1'),
)->setLimit(1);

$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();

return $category instanceof CategoryEntity ? $category : null;
}

/**
* Gets the first found category with the provided name.
* Gets the root category of the shop or.
*/
public function getByName(string $name): ?CategoryEntity
public function getRootCategory(): ?CategoryEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $name));
$criteria->setLimit(1);
return once(function () {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('autoIncrement', 1))
->addFilter(new EqualsFilter('level', 1))
->setLimit(1);

$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();
$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();

return $category instanceof CategoryEntity ? $category : null;
return $category instanceof CategoryEntity ? $category : null;
});
}
}

0 comments on commit 8dd4d83

Please sign in to comment.