Skip to content

Commit

Permalink
Handle product, variant, and translatable updates
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 29, 2024
1 parent 36b9090 commit 94b6cd4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/EventListener/Doctrine/ProductListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Setono\SyliusPeakPlugin\Model\ProductVariantInterface;
use Setono\SyliusPeakPlugin\Workflow\UploadProductVariantRequestWorkflow;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTranslationInterface;
use Sylius\Component\Core\Model\ProductVariantInterface as BaseProductVariantInterface;
use Sylius\Component\Product\Model\ProductVariantTranslationInterface;
use Symfony\Component\Workflow\WorkflowInterface;
use Webmozart\Assert\Assert;

Expand All @@ -34,12 +36,20 @@ public function preUpdate(LifecycleEventArgs $eventArgs): void
private function handle(LifecycleEventArgs $eventArgs): void
{
$obj = $eventArgs->getObject();
if (!$obj instanceof BaseProductVariantInterface && !$obj instanceof ProductInterface) {

/** @psalm-suppress UndefinedInterfaceMethod */
$variants = match (true) {
$obj instanceof ProductInterface => $obj->getVariants(),
$obj instanceof ProductTranslationInterface => $obj->getTranslatable()->getVariants(),
$obj instanceof ProductVariantInterface => [$obj],
$obj instanceof ProductVariantTranslationInterface => [$obj->getTranslatable()],
default => [],
};

if (!is_iterable($variants) || !is_countable($variants) || count($variants) === 0) {
return;
}

$variants = $obj instanceof ProductInterface ? $obj->getVariants() : [$obj];

/** @var BaseProductVariantInterface|ProductVariantInterface $variant */
foreach ($variants as $variant) {
Assert::isInstanceOf($variant, ProductVariantInterface::class);
Expand Down

0 comments on commit 94b6cd4

Please sign in to comment.