Skip to content

Commit

Permalink
Provide post-CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Jun 28, 2023
1 parent 3100240 commit 13c5655
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions features/locale/managing_locales/removing_locale.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Removing locales
And I am logged in as an administrator

@ui @api
Scenario: The developer tries to delete a not used locale
Scenario: Deleting unused locale
Given the store has a product "T-Shirt banana"
And this product is named "Banana T-Shirt with Minions" in the "English (United States)" locale
And this product has no translation in the "Polish (Poland)" locale
Expand All @@ -20,7 +20,7 @@ Feature: Removing locales
And only the "English (United States)" locale should be present in the system

@ui @api
Scenario: The developer tries to deleted a locale in usage
Scenario: Deleting a locale in use
Given the store has a product "T-Shirt banana"
And this product is named "Banana T-Shirt with Minions" in the "English (United States)" locale
And this product is named "Koszulka Banan z Minionami" in the "Polish (Poland)" locale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ public function __construct(
) {
}

/**
* @param array<array-key, mixed> $context
*/
public function supports($data, array $context = []): bool
{
return $data instanceof LocaleInterface;
}

public function persist($data, array $context = [])
/**
* @param array<array-key, mixed> $context
*/
public function persist($data, array $context = []): object
{
return $this->decoratedDataPersister->persist($data, $context);
}

/**
* @param LocaleInterface $data
* @param array<array-key, mixed> $context
*
* @throws LocaleIsUsedException
*/
public function remove($data, array $context = [])
public function remove($data, array $context = []): mixed
{
if ($this->localeUsageChecker->isUsed($data->getCode())) {
throw new LocaleIsUsedException($data->getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function it_persists_locale(
ContextAwareDataPersisterInterface $decoratedDataPersister,
Locale $locale,
): void {
$decoratedDataPersister->persist($locale, [])->shouldBeCalled();
$decoratedDataPersister->persist($locale, [])->shouldBeCalled()->willReturn(new stdClass());

$this->persist($locale);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Sylius/Bundle/LocaleBundle/Checker/LocaleUsageChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
namespace Sylius\Bundle\LocaleBundle\Checker;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Sylius\Component\Locale\Context\LocaleNotFoundException;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Metadata\RegistryInterface;
use Sylius\Component\Resource\Model\TranslationInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class LocaleUsageChecker implements LocaleUsageCheckerInterface
{
/**
* @param RepositoryInterface<LocaleInterface> $localeRepository
*/
public function __construct(
private RepositoryInterface $localeRepository,
private RegistryInterface $resourceRegistry,
Expand Down Expand Up @@ -67,8 +73,12 @@ private function getTranslationEntityInterfaces(): array
return $translationEntityInterfaces;
}

/**
* @param class-string<TranslationInterface> $translationEntityInterface
*/
private function isLocaleUsedByTranslation(string $translationEntityInterface, string $localeCode): bool
{
/** @var EntityRepository<TranslationInterface> $repository */
$repository = $this->entityManager->getRepository($translationEntityInterface);

return $repository->count(['locale' => $localeCode]) > 0;
Expand Down

0 comments on commit 13c5655

Please sign in to comment.