Skip to content

Commit

Permalink
Move out all locale & language related methods of the sales channel f…
Browse files Browse the repository at this point in the history
…ixture
  • Loading branch information
jkniest committed Jun 3, 2024
1 parent 2d1af0d commit 6b65f73
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 77 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ public function __construct(
private ShippingMethodUtils $shippingMethodUtils,
private SalutationUtils $salutationUtils,
private DatabaseUtils $databaseUtils,
private LanguageAndLocaleUtils $languageAndLocaleUtils,
) {
}

Expand Down Expand Up @@ -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.
Expand Down
126 changes: 126 additions & 0 deletions src/Utils/LanguageAndLocaleUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;

use Doctrine\Inflector\Language;
use Shopware\Core\Framework\Context;
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\Language\LanguageCollection;
use Shopware\Core\System\Language\LanguageEntity;
use Shopware\Core\System\Locale\LocaleCollection;
use Shopware\Core\System\Locale\LocaleEntity;
use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetCollection;
use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetEntity;

/**
* This class provides utility methods to work with languages & locales. 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->LanguageAndLocale()->……();
* ```
*/
readonly class LanguageAndLocaleUtils
{
/**
* @param EntityRepository<SnippetSetCollection> $snippetSetRepository
* @param EntityRepository<CountryCollection> $countryRepository
* @param EntityRepository<LanguageCollection> $languageRepository
* @param EntityRepository<LocaleCollection> $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;
});
}
}
74 changes: 0 additions & 74 deletions src/Utils/SalesChannelUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,21 +29,13 @@
{
/**
* @param EntityRepository<SalesChannelCollection> $salesChannelRepository
* @param EntityRepository<SnippetSetCollection> $snippetSetRepository
* @param EntityRepository<TaxCollection> $taxRepository
* @param EntityRepository<CountryCollection> $countryRepository
* @param EntityRepository<LanguageCollection> $languageRepository
* @param EntityRepository<CurrencyCollection> $currencyRepository
* @param EntityRepository<LocaleCollection> $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,
) {
}

Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 6b65f73

Please sign in to comment.