<?php
declare(strict_types=1);
namespace App\EventSubscriber\Product;
use App\Entity\Product\Product;
use App\Handler\ProductVariant\GloballyStockedVariantHandler;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class ProductGlobalStockSubscriber implements EventSubscriberInterface
{
private GloballyStockedVariantHandler $variantHandler;
public function __construct(GloballyStockedVariantHandler $variantHandler)
{
$this->variantHandler = $variantHandler;
}
public static function getSubscribedEvents(): array
{
return [
'sylius.product.pre_create' => 'updateVariantsStock',
'sylius.product.pre_update' => 'updateVariantsStock',
];
}
public function updateVariantsStock(ResourceControllerEvent $controllerEvent): void
{
$product = $controllerEvent->getSubject();
/**
* @var Product $product
*/
$variants = $product->getVariants();
foreach ($variants as $variant)
{
$this->variantHandler->updateVariantStock($variant);
}
//boucle sur les variantes
//service 3
}
}
//subscriber qui va boucler dans les produits (en param il reçoit un produit)
//service1 : retourne si une variante doit etre mise à jour ou pas (resolver) variant/globallyStockedVariantResolver (à reflechir)
//service2 : calcul le nombre de stock possible pour la variante en fonction du stock global passé en param et l'option (provider), variants/PossibleStockQuantityProvider
//service3 : une variante en entrée, il appelle le service 1, si service 1 = non on sort, si egal oui on appelle le service 2 pour le stock (onhand)
// et il defini le nouveau onhand et mettre en base (handler) /variant/globallyStockedVariantHandler