Skip to content

Commit

Permalink
Skip locales not used in any channels also for immutable slug value h…
Browse files Browse the repository at this point in the history
…andler (#137)
  • Loading branch information
lruozzi9 committed Aug 4, 2022
1 parent b62d317 commit 9bf52ec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
35 changes: 34 additions & 1 deletion spec/ValueHandler/ImmutableSlugValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace spec\Webgriffe\SyliusAkeneoPlugin\ValueHandler;

use Cocur\Slugify\SlugifyInterface;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTranslationInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
Expand All @@ -28,12 +31,28 @@ function let(
SlugifyInterface $slugify,
FactoryInterface $productTranslationFactory,
TranslationLocaleProviderInterface $translationLocaleProvider,
RepositoryInterface $productTranslationRepository
RepositoryInterface $productTranslationRepository,
LocaleInterface $italianLocale,
LocaleInterface $englishLocale,
ChannelInterface $commerceChannel,
ChannelInterface $supportChannel,
ProductInterface $product
) {
$slugify->slugify(self::VALUE_TO_SLUGIFY)->willReturn(self::SLUGIFIED_VALUE);
$translationLocaleProvider->getDefinedLocalesCodes()->willReturn(['en_US', 'it_IT']);
$productTranslationRepository->findOneBy(['slug' => self::SLUGIFIED_VALUE, 'locale' => 'en_US'])->willReturn(null);
$productTranslationRepository->findOneBy(['slug' => self::SLUGIFIED_VALUE, 'locale' => 'it_IT'])->willReturn(null);

$italianLocale->getCode()->willReturn('it_IT');
$englishLocale->getCode()->willReturn('en_US');

$commerceChannel->getCode()->willReturn('ecommerce');
$commerceChannel->getLocales()->willReturn(new ArrayCollection([$italianLocale->getWrappedObject()]));
$supportChannel->getCode()->willReturn('support');
$supportChannel->getLocales()->willReturn(new ArrayCollection([$italianLocale->getWrappedObject(), $englishLocale->getWrappedObject()]));

$product->getChannels()->willReturn(new ArrayCollection([$commerceChannel->getWrappedObject(), $supportChannel->getWrappedObject()]));

$this->beConstructedWith(
$slugify,
$productTranslationFactory,
Expand Down Expand Up @@ -204,4 +223,18 @@ function it_skips_locales_not_specified_in_sylius(

$productTranslation->setSlug(Argument::type('string'))->shouldNotHaveBeenCalled();
}

public function it_skips_locales_not_used_in_any_product_channels(
ProductVariantInterface $productVariant,
ProductInterface $product,
ProductTranslationInterface $productTranslation,
ChannelInterface $commerceChannel
): void {
$product->getChannels()->willReturn(new ArrayCollection([$commerceChannel->getWrappedObject()]));
$productVariant->getProduct()->willReturn($product);

$this->handle($productVariant, self::AKENEO_ATTRIBUTE, [['locale' => 'en_US', 'scope' => null, 'data' => 'New value']]);

$productTranslation->setSlug(Argument::type('string'))->shouldNotHaveBeenCalled();
}
}
18 changes: 18 additions & 0 deletions src/ValueHandler/ImmutableSlugValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Webgriffe\SyliusAkeneoPlugin\ValueHandler;

use Cocur\Slugify\SlugifyInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTranslationInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
Expand Down Expand Up @@ -85,6 +86,9 @@ public function handle($subject, string $attribute, array $value): void
if (!in_array($localeCode, $this->translationLocaleProvider->getDefinedLocalesCodes(), true)) {
continue;
}
if (!$this->isLocaleUsedInAtLeastOneChannelForTheProduct($product, $localeCode)) {
continue;
}

$productTranslation = $this->getOrCreateNewProductTranslation($product, $localeCode);
if ($productTranslation->getSlug() !== null) {
Expand Down Expand Up @@ -150,4 +154,18 @@ private function getDeduplicatedSlug(

return $deduplicatedSlug;
}

private function isLocaleUsedInAtLeastOneChannelForTheProduct(ProductInterface $product, string $localeCode): bool
{
foreach ($product->getChannels() as $channel) {
Assert::isInstanceOf($channel, ChannelInterface::class);
foreach ($channel->getLocales() as $locale) {
if ($locale->getCode() === $localeCode) {
return true;
}
}
}

return false;
}
}

0 comments on commit 9bf52ec

Please sign in to comment.