Browse Source

Envoi notif mattermost depuis listener sur postPersist

François 6 năm trước cách đây
mục cha
commit
7b22b521c2

+ 5 - 1
app/config/services.yml

@@ -61,4 +61,8 @@ services:
     AppBundle\Service\UserManager:
         arguments:
         - "@doctrine.orm.entity_manager"
-        - "@security.password_encoder"
+        - "@security.password_encoder"
+
+    AppBundle\Service\FilmCreationListener:
+        tags:
+        - { name: doctrine.event_listener, event: postPersist }

+ 1 - 4
src/AppBundle/Controller/VideothequeController.php

@@ -3,9 +3,7 @@
 namespace AppBundle\Controller;
 
 use AppBundle\Entity\Commentaire;
-use AppBundle\Entity\Genre;
 use AppBundle\Entity\Realisateur;
-use AppBundle\Service\Mattermost;
 use AppBundle\Service\UniciteCollections;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@@ -70,7 +68,7 @@ class VideothequeController extends Controller
 	/**
 	 * @Route("/ajouter", name="videotheque_ajouter")
 	 */
-	public function ajouterAction(Request $request, Mattermost $mattermost, UniciteCollections $uniciteCollections)
+	public function ajouterAction(Request $request, UniciteCollections $uniciteCollections)
 	{
 		$film = new Film;
 		$film->setAuthered($this->getUser());
@@ -84,7 +82,6 @@ class VideothequeController extends Controller
 			$em->persist($film);
 			$em->flush();
 			$this->addFlash('success', 'Le film a été ajouté');
-			$mattermost->sendNouveauFilm($film);
 			return $this->redirectToRoute('videotheque_voirfilm', array('id'=>$film->getId()));
 		}
 

+ 37 - 0
src/AppBundle/Service/FilmCreationListener.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace AppBundle\Service;
+
+use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
+use AppBundle\Entity\Film;
+use Symfony\Component\Config\Definition\Exception\Exception;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class FilmCreationListener
+{
+    /**
+     * var mattermostSender
+     */
+    private $mattermostSender;
+    private $session;
+
+    public function __construct(Mattermost $mattermostSender)
+    {
+        $this->mattermostSender = $mattermostSender;
+    }
+
+    public function postPersist(LifecycleEventArgs $args)
+    {
+        $entity = $args->getObject();
+        if (!$entity instanceof Film)
+        {
+            return;
+        }
+
+        try {
+            $this->mattermostSender->sendNouveauFilm($entity);
+        } catch (Exception $exception) {
+            // Ajouter flashbag
+        }
+    }
+}