Skip to content

Commit

Permalink
Fix bug in create upload product variant requests
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 16, 2024
1 parent 9b50ea7 commit 3d3dcdd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,52 @@

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\EventListener\Doctrine;
namespace Setono\SyliusPeakPlugin\EventSubscriber;

use Doctrine\Persistence\Event\LifecycleEventArgs;
use Setono\SyliusPeakPlugin\Factory\UploadProductVariantRequestFactoryInterface;
use Setono\SyliusPeakPlugin\Model\ProductVariantInterface;
use Setono\SyliusPeakPlugin\Workflow\UploadProductVariantRequestWorkflow;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
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\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\WorkflowInterface;
use Webmozart\Assert\Assert;

final class ProductListener
final class CreateUploadProductVariantRequestSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly UploadProductVariantRequestFactoryInterface $uploadProductVariantRequestFactory,
private readonly WorkflowInterface $uploadProductVariantRequestWorkflow,
) {
}

public function prePersist(LifecycleEventArgs $eventArgs): void
public static function getSubscribedEvents(): array
{
$this->handle($eventArgs);
return [
'sylius.product.pre_create' => 'handle',
'sylius.product.pre_update' => 'handle',
'sylius.product_variant.pre_create' => 'handle',
'sylius.product_variant.pre_update' => 'handle',
];
}

public function preUpdate(LifecycleEventArgs $eventArgs): void
public function handle(ResourceControllerEvent $event): void
{
$this->handle($eventArgs);
}

private function handle(LifecycleEventArgs $eventArgs): void
{
$obj = $eventArgs->getObject();
/** @var mixed $obj */
$obj = $event->getSubject();

/** @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) {
if (!is_countable($variants)) {
throw new \LogicException('The variants must be iterable and countable.');
}

if (count($variants) === 0) {
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<import resource="services/command.xml"/>
<import resource="services/controller.xml"/>
<import resource="services/data_mapper.xml"/>
<import resource="services/event_listener.xml"/>
<import resource="services/event_subscriber.xml"/>
<import resource="services/factory.xml"/>
<import resource="services/message.xml"/>
Expand Down
13 changes: 0 additions & 13 deletions src/Resources/config/services/event_listener.xml

This file was deleted.

7 changes: 7 additions & 0 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="Setono\SyliusPeakPlugin\EventSubscriber\CreateUploadProductVariantRequestSubscriber">
<argument type="service" id="setono_sylius_peak.factory.upload_product_variant_request"/>
<argument type="service" id="state_machine.setono_sylius_peak__upload_product_variant_request"/>

<tag name="kernel.event_subscriber"/>
</service>

<service id="Setono\SyliusPeakPlugin\EventSubscriber\HandleOrderCancellationSubscriber">
<argument type="service" id="Setono\PeakWMS\Client\ClientInterface"/>

Expand Down

0 comments on commit 3d3dcdd

Please sign in to comment.