From 6b65f738137f76a9f957b74f2b771435c9af7d1a Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Mon, 3 Jun 2024 19:28:28 +0200 Subject: [PATCH] Move out all locale & language related methods of the sales channel fixture --- CHANGELOG.md | 10 ++- src/FixtureHelper.php | 11 +++ src/Utils/LanguageAndLocaleUtils.php | 126 +++++++++++++++++++++++++++ src/Utils/SalesChannelUtils.php | 74 ---------------- 4 files changed, 144 insertions(+), 77 deletions(-) create mode 100644 src/Utils/LanguageAndLocaleUtils.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 83e14f6..e5fd971 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,15 +24,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `FixtureTrait::runFixtureGroup` is a new function to execute whole fixture groups with optionally dependencies - Each fixture now has direct access to the FixtureHelper using `$this->helper` - **Breaking** If you have the helper (or any other helper) previously assigned to `$this->helper` it will either fail or override the FixturePlugin helper +- **Breaking** Moved `SalesChannelUtils::getLanguage()` to `LanguageAndLocaleUtils::getLanguage()` +- **Breaking** Moved `SalesChannelUtils::getLocale()` to `LanguageAndLocaleUtils::getLocale()` +- **Breaking** Moved `SalesChannelUtils::getCountry()` to `LanguageAndLocaleUtils::getCountry()` +- **Breaking** Moved `SalesChannelUtils::getSnippetSet()` to `LanguageAndLocaleUtils::getSnippetSet()` ### Removed - Dropped support for PHP 8.1 - Dropped support for Shopware 6.3 & 6.4 -- Removed FixtureBag -- CategoryUtils +- **Breaking** Removed FixtureBag +- **Breaking** CategoryUtils - Removed method `getFirst` on CategoryUtils - Removed method `getByName` on CategoryUtils -- Renamed `CategoryUtils` to `SalutationUtils` +- **Breaking** Renamed `CategoryUtils` to `SalutationUtils` ## [2.4.0] - 2023-11-15 ### Added diff --git a/src/FixtureHelper.php b/src/FixtureHelper.php index 98d4bd7..58367fa 100644 --- a/src/FixtureHelper.php +++ b/src/FixtureHelper.php @@ -7,6 +7,7 @@ use Basecom\FixturePlugin\Utils\CategoryUtils; use Basecom\FixturePlugin\Utils\CmsUtils; use Basecom\FixturePlugin\Utils\DatabaseUtils; +use Basecom\FixturePlugin\Utils\LanguageAndLocaleUtils; use Basecom\FixturePlugin\Utils\MediaUtils; use Basecom\FixturePlugin\Utils\PaymentMethodUtils; use Basecom\FixturePlugin\Utils\SalesChannelUtils; @@ -24,6 +25,7 @@ public function __construct( private ShippingMethodUtils $shippingMethodUtils, private SalutationUtils $salutationUtils, private DatabaseUtils $databaseUtils, + private LanguageAndLocaleUtils $languageAndLocaleUtils, ) { } @@ -90,6 +92,15 @@ public function ShippingMethod(): ShippingMethodUtils return $this->shippingMethodUtils; } + /** + * Use this to access the language & locale related features + * of the fixture helper class. + */ + public function LanguageAndLocale(): LanguageAndLocaleUtils + { + return $this->languageAndLocaleUtils; + } + /** * Use this to access the general database helper functions * of the fixture helper class. diff --git a/src/Utils/LanguageAndLocaleUtils.php b/src/Utils/LanguageAndLocaleUtils.php new file mode 100644 index 0000000..7ccefe3 --- /dev/null +++ b/src/Utils/LanguageAndLocaleUtils.php @@ -0,0 +1,126 @@ +helper->LanguageAndLocale()->……(); + * ``` + */ +readonly class LanguageAndLocaleUtils +{ + /** + * @param EntityRepository $snippetSetRepository + * @param EntityRepository $countryRepository + * @param EntityRepository $languageRepository + * @param EntityRepository $localeRepository + */ + public function __construct( + private EntityRepository $snippetSetRepository, + private EntityRepository $countryRepository, + private EntityRepository $languageRepository, + private EntityRepository $localeRepository, + ) { + } + + /** + * Return a specific language by its name or null if its was not found. + */ + public function getLanguage(string $languageName): ?LanguageEntity + { + return once(function () use ($languageName): ?LanguageEntity { + $criteria = (new Criteria())->addFilter( + new EqualsFilter('name', $languageName), + )->setLimit(1); + + $criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__)); + + $language = $this->languageRepository + ->search($criteria, Context::createDefaultContext()) + ->first(); + + return $language instanceof LanguageEntity ? $language : null; + }); + } + + /** + * Return a specific locale by its ISO code or null if its was not found. + */ + public function getLocale(string $code): ?LocaleEntity + { + return once(function () use ($code): ?LocaleEntity { + $criteria = (new Criteria())->addFilter( + new EqualsFilter('code', $code), + )->setLimit(1); + + $criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__)); + + $locale = $this->localeRepository + ->search($criteria, Context::createDefaultContext()) + ->first(); + + return $locale instanceof LocaleEntity ? $locale : null; + }); + } + + /** + * Return a specific country by its ISO code or null if its was not found. + */ + public function getCountry(string $countryIso): ?CountryEntity + { + return once(function () use ($countryIso): ?CountryEntity { + $criteria = (new Criteria())->addFilter( + new EqualsFilter('iso', $countryIso), + )->setLimit(1); + + $criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__)); + + $country = $this->countryRepository + ->search($criteria, Context::createDefaultContext()) + ->first(); + + return $country instanceof CountryEntity ? $country : null; + }); + } + + /** + * Return a specific snippet set by its country's ISO code or null if its was not found. + */ + public function getSnippetSet(string $countryCodeIso): ?SnippetSetEntity + { + return once(function () use ($countryCodeIso): ?SnippetSetEntity { + $criteria = (new Criteria())->addFilter( + new EqualsFilter('iso', $countryCodeIso), + )->setLimit(1); + + $criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__)); + + $snippetSet = $this->snippetSetRepository + ->search($criteria, Context::createDefaultContext()) + ->first(); + + return $snippetSet instanceof SnippetSetEntity ? $snippetSet : null; + }); + } +} diff --git a/src/Utils/SalesChannelUtils.php b/src/Utils/SalesChannelUtils.php index 6d3bddc..79337b0 100644 --- a/src/Utils/SalesChannelUtils.php +++ b/src/Utils/SalesChannelUtils.php @@ -9,18 +9,10 @@ use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; -use Shopware\Core\System\Country\CountryCollection; -use Shopware\Core\System\Country\CountryEntity; use Shopware\Core\System\Currency\CurrencyCollection; use Shopware\Core\System\Currency\CurrencyEntity; -use Shopware\Core\System\Language\LanguageCollection; -use Shopware\Core\System\Language\LanguageEntity; -use Shopware\Core\System\Locale\LocaleCollection; -use Shopware\Core\System\Locale\LocaleEntity; use Shopware\Core\System\SalesChannel\SalesChannelCollection; use Shopware\Core\System\SalesChannel\SalesChannelEntity; -use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetCollection; -use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetEntity; use Shopware\Core\System\Tax\TaxCollection; use Shopware\Core\System\Tax\TaxEntity; @@ -37,21 +29,13 @@ { /** * @param EntityRepository $salesChannelRepository - * @param EntityRepository $snippetSetRepository * @param EntityRepository $taxRepository - * @param EntityRepository $countryRepository - * @param EntityRepository $languageRepository * @param EntityRepository $currencyRepository - * @param EntityRepository $localeRepository */ public function __construct( private EntityRepository $salesChannelRepository, - private EntityRepository $snippetSetRepository, private EntityRepository $taxRepository, - private EntityRepository $countryRepository, - private EntityRepository $languageRepository, private EntityRepository $currencyRepository, - private EntityRepository $localeRepository, ) { } @@ -110,64 +94,6 @@ public function getCurrencyEuro(): ?CurrencyEntity return $currency instanceof CurrencyEntity ? $currency : null; } - // TODO: Move to LanguageAndLocaleUtils - public function getLanguage(string $languageName): ?LanguageEntity - { - $criteria = (new Criteria())->addFilter( - new EqualsFilter('name', $languageName), - )->setLimit(1); - - $language = $this->languageRepository - ->search($criteria, Context::createDefaultContext()) - ->first(); - - return $language instanceof LanguageEntity ? $language : null; - } - - // TODO: Move to LanguageAndLocaleUtils - public function getLocale(string $code): ?LocaleEntity - { - $criteria = (new Criteria())->addFilter( - new EqualsFilter('code', $code), - )->setLimit(1); - - $locale = $this->localeRepository - ->search($criteria, Context::createDefaultContext()) - ->first(); - - return $locale instanceof LocaleEntity ? $locale : null; - } - - // TODO: Move to LanguageAndLocaleUtils - public function getCountry(string $countryIso): ?CountryEntity - { - $criteria = (new Criteria())->addFilter( - new EqualsFilter('iso', $countryIso), - )->setLimit(1); - - $country = $this->countryRepository - ->search( - $criteria, - Context::createDefaultContext(), - )->first(); - - return $country instanceof CountryEntity ? $country : null; - } - - // TODO: Move to LanguageAndLocaleUtils - public function getSnippetSet(string $countryCodeIso): ?SnippetSetEntity - { - $criteria = (new Criteria())->addFilter( - new EqualsFilter('iso', $countryCodeIso), - )->setLimit(1); - - $snippetSet = $this->snippetSetRepository - ->search($criteria, Context::createDefaultContext()) - ->first(); - - return $snippetSet instanceof SnippetSetEntity ? $snippetSet : null; - } - // TODO: Move to TaxUtils public function getTax19(): ?TaxEntity {