src/Listeners/ImageListener.php line 36

Open in your IDE?
  1. <?php
  2.  
  3. namespace App\Listeners;
  4.  
  5. use Doctrine\ORM\Event\LifecycleEventArgs;
  6. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  7. use App\Entity\Image;
  8. use App\Entity\EStateAd;
  9. use App\Services\ImageTransformer;
  10. use Vich\UploaderBundle\Event\Event;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13.  
  14. /**
  15.  * ImageListener
  16.  */
  17. class ImageListener
  18. {
  19.     private $cacheManager;
  20.     private $path_image;
  21.     private $orm;
  22.     private $mailer;
  23.     private $templating;
  24.     private $requestStack;
  25.  
  26.     public function __construct(CacheManager $cacheManagerEntityManagerInterface $ormstring $path_image, \Swift_Mailer $mailer, \Twig_Environment $templatingRequestStack $requestStack)
  27.     {
  28.         $this->mailer $mailer;
  29.         $this->cacheManager $cacheManager;
  30.         $this->path_image $path_image;
  31.         $this->orm $orm;
  32.         $this->templating $templating;
  33.         $this->requestStack $requestStack;
  34.     }
  35.  
  36.     public function onVichUploaderPreInject(Event $args)
  37.     {
  38.         $entity $args->getObject();
  39.  
  40.         if (!$entity instanceof Image) {
  41.             return;
  42.         }
  43.  
  44.         $image $entity->getImage();
  45.         $entity->setTmpFile($image);
  46.         $this->orm->flush();
  47.  
  48.     }
  49.  
  50.  
  51.     public function postUpdate(LifecycleEventArgs $args)
  52.     {
  53.         $entity $args->getEntity();
  54.  /*dd($entity instanceof EStateAd);*/
  55.         if (!$entity instanceof EStateAd) {
  56.             return;
  57.         }
  58.         $changeSet $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($entity);
  59.  /*dd($changeSet);*/
  60.         if(!array_key_exists("estatead_image1"$changeSet)){ 
  61.         return;
  62.         }
  63.         
  64.         $entity->setStatus(0);
  65.         $date_j2 = new \DateTime('now');
  66.         $date_j2->add(new \DateInterval('P2D'));
  67.         $entity->setExpirationDate($date_j2);
  68.         $message2 = (new \Swift_Message('[PREUMS] Edition d\'une annonce'))
  69.             ->setFrom('postmaster@nouveausite.fr')
  70.             ->setTo(['alexrush@gmail.com''gerald.seguin@gustibus.fr'])
  71.             ->setBody(
  72.                 $this->templating->render(
  73.                     // templates/emails/registration.html.twig
  74.                     'emails/estatead_edition.html.twig',
  75.                     []
  76.                 ),
  77.                 'text/html'
  78.             )
  79.         ;
  80.         if(!$this->mailer->send($message2$failures))
  81.         {
  82.             dd($failures);
  83.         }
  84.         
  85.         if($this->requestStack->getCurrentRequest() != null){
  86.             $session $this->requestStack->getCurrentRequest()->getSession();
  87.             $session->getFlashBag()->add('success''estatead_edit_validation');
  88.         }
  89.   
  90.         try {
  91.             $this->cacheManager->remove($this->path_image.'/'.$entity->getEStateAdImage1());
  92.             $this->cacheManager->resolve($this->path_image.'/'.$entity->getEStateAdImage1(), null);
  93.  
  94.         } catch (\Exception $e) {
  95.  
  96.         }
  97.  
  98.     }
  99.  
  100.     public function preRemove(LifecycleEventArgs $args)
  101.     {
  102.         $entity $args->getEntity();
  103.  
  104.         if (!$entity instanceof Image) {
  105.             return;
  106.         }
  107.  
  108.         $target $this->path_image.'/'.$entity->getImage();
  109.         try {
  110.             $this->cacheManager->remove($target);
  111.         } catch (\Exception $e) {
  112.  
  113.         }
  114.     }
  115.  
  116.     public function postPersist(LifecycleEventArgs $args)
  117.     {
  118.         $entity $args->getObject();
  119.  
  120.         if (!$entity instanceof Image) {
  121.             return;
  122.         }
  123.         $file $this->path_image.'/'.$entity->getImage();
  124.     }
  125. }