src/EventSubscriber/Product/ProductResourceSubscriber.php line 25

  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\Entity\Product\Product;
  11. use App\Repository\Channel\ChannelEcodaAccountInfoRepository;
  12. use App\Repository\Channel\ChannelEcodaAccountTypeRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use App\Manager\EcodaAccountManager;
  17. use App\Entity\Product\ProductVariant;
  18. class ProductResourceSubscriber  implements EventSubscriberInterface
  19.      
  20.     public function onPreCreate(GenericEvent $event)
  21.     {  
  22.         $entity $event->getSubject();  
  23.         Assert::isInstanceOf($entityProduct::class); 
  24.         $this->updateDimensions($entity);
  25.     } 
  26.     
  27.     public function onPreUpdate(GenericEvent $event)
  28.     {  
  29.         $entity $event->getSubject();  
  30.         Assert::isInstanceOf($entityProduct::class); 
  31.        
  32.         $this->updateDimensions($entity);
  33.     } 
  34.     private function updateDimensions($entity){ 
  35.         if($entity->getVariants()[0] !== null  && ($dimension $entity->getVariants()[0]->getDimension())){
  36.             $entity->getVariants()[0]->setHeight($dimension->getHeight());
  37.             $entity->getVariants()[0]->setWidth($dimension->getWidth());
  38.             $entity->getVariants()[0]->setDepth($dimension->getDepth());
  39.             $entity->getVariants()[0]->setMesurementUnit($dimension->getUnit()->getCode());
  40.         } 
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.            'sylius.product.pre_create' => 'onPreCreate'
  46.            'sylius.product.pre_update' => 'onPreUpdate'
  47.         ];
  48.     }
  49. }