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.      
  21.     public function onPreCreate(GenericEvent $event)
  22.     {  
  23.         $entity = $event->getSubject();  
  24.         Assert::isInstanceOf($entity, Product::class); 
  25.         $this->updateDimensions($entity);
  26.     } 
  27.     
  28.     public function onPreUpdate(GenericEvent $event)
  29.     {  
  30.         $entity = $event->getSubject();  
  31.         Assert::isInstanceOf($entity, Product::class); 
  32.        
  33.         $this->updateDimensions($entity);
  34.     } 
  35.     private function updateDimensions($entity){ 
  36.         if($entity->getVariants()[0] !== null  && ($dimension = $entity->getVariants()[0]->getDimension())){
  37.             $entity->getVariants()[0]->setHeight($dimension->getHeight());
  38.             $entity->getVariants()[0]->setWidth($dimension->getWidth());
  39.             $entity->getVariants()[0]->setDepth($dimension->getDepth());
  40.             $entity->getVariants()[0]->setMesurementUnit($dimension->getUnit()->getCode());
  41.         } 
  42.     }
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.            'sylius.product.pre_create' => 'onPreCreate', 
  47.            'sylius.product.pre_update' => 'onPreUpdate', 
  48.         ];
  49.     }
  50. }