<?php
namespace App\Listeners;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use App\Entity\Image;
use App\Entity\EStateAd;
use App\Services\ImageTransformer;
use Vich\UploaderBundle\Event\Event;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* ImageListener
*/
class ImageListener
{
private $cacheManager;
private $path_image;
private $orm;
private $mailer;
private $templating;
private $requestStack;
public function __construct(CacheManager $cacheManager, EntityManagerInterface $orm, string $path_image, \Swift_Mailer $mailer, \Twig_Environment $templating, RequestStack $requestStack)
{
$this->mailer = $mailer;
$this->cacheManager = $cacheManager;
$this->path_image = $path_image;
$this->orm = $orm;
$this->templating = $templating;
$this->requestStack = $requestStack;
}
public function onVichUploaderPreInject(Event $args)
{
$entity = $args->getObject();
if (!$entity instanceof Image) {
return;
}
$image = $entity->getImage();
$entity->setTmpFile($image);
$this->orm->flush();
}
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
/*dd($entity instanceof EStateAd);*/
if (!$entity instanceof EStateAd) {
return;
}
$changeSet = $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($entity);
/*dd($changeSet);*/
if(!array_key_exists("estatead_image1", $changeSet)){
return;
}
$entity->setStatus(0);
$date_j2 = new \DateTime('now');
$date_j2->add(new \DateInterval('P2D'));
$entity->setExpirationDate($date_j2);
$message2 = (new \Swift_Message('[PREUMS] Edition d\'une annonce'))
->setFrom('postmaster@nouveausite.fr')
->setTo(['alexrush@gmail.com', 'gerald.seguin@gustibus.fr'])
->setBody(
$this->templating->render(
// templates/emails/registration.html.twig
'emails/estatead_edition.html.twig',
[]
),
'text/html'
)
;
if(!$this->mailer->send($message2, $failures))
{
dd($failures);
}
if($this->requestStack->getCurrentRequest() != null){
$session = $this->requestStack->getCurrentRequest()->getSession();
$session->getFlashBag()->add('success', 'estatead_edit_validation');
}
try {
$this->cacheManager->remove($this->path_image.'/'.$entity->getEStateAdImage1());
$this->cacheManager->resolve($this->path_image.'/'.$entity->getEStateAdImage1(), null);
} catch (\Exception $e) {
}
}
public function preRemove(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if (!$entity instanceof Image) {
return;
}
$target = $this->path_image.'/'.$entity->getImage();
try {
$this->cacheManager->remove($target);
} catch (\Exception $e) {
}
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if (!$entity instanceof Image) {
return;
}
$file = $this->path_image.'/'.$entity->getImage();
}
}