src/EventSubscriber/Product/ProductVariantResourceSubscriber.php line 24

  1. <?php
  2. namespace App\EventSubscriber\Product;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Webmozart\Assert\Assert;
  7. use Sylius\Component\Customer\Model\CustomerInterface;
  8. use App\Repository\Customer\CustomerRepository;
  9. use App\Entity\Channel\ChannelEcodaAccountInfo;
  10. use App\Repository\Channel\ChannelEcodaAccountInfoRepository;
  11. use App\Repository\Channel\ChannelEcodaAccountTypeRepository;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use App\Manager\EcodaAccountManager;
  16. use App\Entity\Product\ProductVariant;
  17. class ProductVariantResourceSubscriber  implements EventSubscriberInterface
  18. { 
  19.      
  20.     public function onSyliusProductVariantPreCreate(GenericEvent $event)
  21.     {  
  22.         $entity = $event->getSubject();  
  23.         Assert::isInstanceOf($entity, ProductVariant::class); 
  24.         $this->updateDimensions($entity);
  25.     } 
  26.     public function onSyliusProductVariantPreUpdate(GenericEvent $event)
  27.     {  
  28.         $entity = $event->getSubject();  
  29.         Assert::isInstanceOf($entity, ProductVariant::class); 
  30.         dd(5);
  31.         $this->updateDimensions($entity);
  32.     } 
  33.     private function updateDimensions($entity){
  34.         if($dimension = $entity->getDimension()){
  35.             $entity->setHeight($dimension->getHeight());
  36.             $entity->setWidth($dimension->getWidth());
  37.             $entity->setDepth($dimension->getDepth());
  38.             $entity->setMesurementUnit($dimension->getUnit()->getCode());
  39.         } 
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.            'sylius.product_variant.pre_create' => 'onSyliusProductVariantPreCreate', 
  45.            'sylius.product_variant.pre_update' => 'onSyliusProductVariantPreUpdate', 
  46.         ];
  47.     }
  48. }