src/EventSubscriber/Quote/RequestForQuoteFileTypeResourceSubscriber.php line 36

  1. <?php
  2. namespace App\EventSubscriber\Quote;
  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\Quote\RequestForQuoteFileType;
  17. use App\Entity\Quote\RequestForQuoteFileTypeBlank;
  18. use App\Entity\Quote\RequestForQuoteFileTypeExample;
  19. class RequestForQuoteFileTypeResourceSubscriber  implements EventSubscriberInterface
  20. { 
  21.     private $request; 
  22.     public function __construct(RequestStack $request)
  23.     {
  24.         $this->request = $request->getCurrentRequest(); 
  25.     }
  26.     public function onSyliusEntityPreCreate(GenericEvent $event)
  27.     {  
  28.         $entity = $event->getSubject();  
  29.         Assert::isInstanceOf($entity, RequestForQuoteFileType::class); 
  30.         $this->uplaodFiles($entity);
  31.     } 
  32.     public function onSyliusEntityPreUpdate(GenericEvent $event)
  33.     {   
  34.         $entity = $event->getSubject();  
  35.         Assert::isInstanceOf($entity, RequestForQuoteFileType::class); 
  36.         $this->uplaodFiles($entity); 
  37.     } 
  38.     private function uplaodFiles($entity){
  39.         $example = $this->request->files->get('request_for_quote_file_type')['example']['file'];
  40.         $blank = $this->request->files->get('request_for_quote_file_type')['blank']['file']; 
  41.       
  42.         if($example){
  43.             $exampleFile = new RequestForQuoteFileTypeExample();
  44.             $exampleFile->setFile($example); 
  45.             $exampleFile->setUpdatedAt(new \DateTime()); 
  46.             $entity->setExample($exampleFile);
  47.         }else{
  48.             $entity->setExample(null);
  49.         }
  50.  
  51.        
  52.         if($blank){
  53.             $blankFile = new RequestForQuoteFileTypeBlank();
  54.             $blankFile->setFile($blank);
  55.             $blankFile->setUpdatedAt(new \DateTime());
  56.             $entity->setBlank($blankFile);
  57.         }else{
  58.             $entity->setBlank(null);
  59.         } 
  60.         //dd($entity);
  61.     }
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.            'app.request_for_quote_file_type.pre_create' => 'onSyliusEntityPreCreate', 
  66.            'app.request_for_quote_file_type.pre_update' => 'onSyliusEntityPreUpdate', 
  67.         ];
  68.     }
  69. }