Browse Source

Ajout d'une notif mattermost nouveau user

François Drouhard 3 years ago
parent
commit
b86da34148
4 changed files with 65 additions and 8 deletions
  1. 1 0
      .env
  2. 8 1
      config/services.yaml
  3. 27 7
      src/Service/Mattermost.php
  4. 29 0
      src/Service/RegisterListener.php

+ 1 - 0
.env

@@ -30,6 +30,7 @@ DATABASE_URL=mysql://user:password@127.0.0.1:3306/nomDeLaBase
 MATTERMOST_URL=https://adresse_du_serveur_mattermost
 MATTERMOST_TOKEN=Token_du_bot_mattermost
 MATTERMOST_CHANNEL_ID=Id_du_channel_dans_lequel_poster
+MATTERMOST_CHANNEL_ID_ADMIN=Id_du_channel_dans_lequel_poster_les_notifs_admin # (Nouvel enregistrement utilisateur)
 ###< Mattermost/Service ###
 
 ###> symfony/mailer ###

+ 8 - 1
config/services.yaml

@@ -40,6 +40,7 @@ services:
             - '%env(MATTERMOST_URL)%'
             - '%env(MATTERMOST_TOKEN)%'
             - '%env(MATTERMOST_CHANNEL_ID)%'
+            - '%env(MATTERMOST_CHANNEL_ID_ADMIN)%'
 
     App\Service\FilmCreationListener:
         arguments:
@@ -57,4 +58,10 @@ services:
         tags:
         - { name: doctrine.event_listener, event: postPersist }
         - { name: doctrine.event_listener, event: postUpdate }
-        - { name: doctrine.event_listener, event: postRemove }
+        - { name: doctrine.event_listener, event: postRemove }
+
+    App\Service\RegisterListener:
+        arguments:
+            - '@App\Service\Mattermost'
+        tags:
+            - { name: doctrine.event_listener, event: postPersist }

+ 27 - 7
src/Service/Mattermost.php

@@ -15,20 +15,22 @@ class Mattermost
     protected $url;
     protected $token;
     protected $channelId;
+    protected $channelIdAdmin;
     protected $router;
 
     /**
      * Mattermost Manager
      */
-    public function __construct($url, $token, $channelId, UrlGeneratorInterface $router)
+    public function __construct($url, $token, $channelId, $channelIdAdmin, UrlGeneratorInterface $router)
     {
         $this->url = $url . "/api/v4/posts";
         $this->token = $token;
         $this->channelId = $channelId;
+        $this->channelIdAdmin = $channelIdAdmin;
         $this->router = $router;
     }
 
-    protected function SendNotif($message)
+    protected function SendNotif($message, $isChanAdmin = False) :void
     {
         // crée une nouvelle ressource cURL
         $ch = curl_init();
@@ -37,7 +39,7 @@ class Mattermost
         $options = array(
             CURLOPT_URL => $this->url,
             CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Authorization: Bearer ' . $this->token),
-            CURLOPT_POSTFIELDS  =>  '{"channel_id": "' . $this->channelId . '", "message": "' . $message . '"}'
+            CURLOPT_POSTFIELDS  =>  '{"channel_id": "' . ($isChanAdmin ? $this->channelIdAdmin : $this->channelId) . '", "message": "' . $message . '"}'
         );
 
         curl_setopt_array($ch, $options);
@@ -49,7 +51,7 @@ class Mattermost
         curl_close($ch);
     }
 
-    public function sendNouveauFilm(Film $film)
+    public function sendNouveauFilm(Film $film) :void
     {
         $message =
             ":new: **"
@@ -57,11 +59,13 @@ class Mattermost
             ."** vient d'ajouter **"
             ."[".$film->getTitre()."]("
             .$this->router->generate('videotheque_voirfilm', ['id' => $film->getId()], UrlGeneratorInterface::ABSOLUTE_URL)
-            .")** dans la [vidéothèque](https://videotheque.fdlibre.eu).";
+            .")** dans la [vidéothèque]("
+            .$this->router->generate('videotheque_liste', [], UrlGeneratorInterface::ABSOLUTE_URL)
+            .").";
         $this->SendNotif($message);
     }
 
-    public function sendNouveauCommentaire(User $user, Film $film)
+    public function sendNouveauCommentaire(User $user, Film $film) :void
     {
         $message =
             ":new: **"
@@ -69,10 +73,26 @@ class Mattermost
             ."** vient d'ajouter un commentaire sur le film **"
             ."[".$film->getTitre()."]("
             .$this->router->generate('videotheque_voirfilm', ['id' => $film->getId()], UrlGeneratorInterface::ABSOLUTE_URL)
-            .")** dans la [vidéothèque](https://videotheque.fdlibre.eu).";
+            .")** dans la [vidéothèque]("
+            .$this->router->generate('videotheque_liste', [], UrlGeneratorInterface::ABSOLUTE_URL)
+            .").";
 
         $this->SendNotif($message);
     }
 
+    public function sendNewUser($userName) : void
+    {
+        $message =
+        ":warning: Un utilisateur vient de s'enregistrer dans la [vidéothèque]("
+        .$this->router->generate('videotheque_liste', [], UrlGeneratorInterface::ABSOLUTE_URL)
+        .")** : "
+        .$userName
+        ."**. Accéder à la [liste des utilisateurs]("
+        .$this->router->generate('admin_index', [],  UrlGeneratorInterface::ABSOLUTE_URL)
+        .").";
+
+        $this->sendNotif($message, True);
+    }
+
 
 }

+ 29 - 0
src/Service/RegisterListener.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Service;
+
+use App\Entity\User;
+use Doctrine\Persistence\Event\LifecycleEventArgs;
+
+
+class RegisterListener
+{
+    private $mattermostSender;
+
+    public function __construct(Mattermost $mattermostSender)
+    {
+        $this->mattermostSender = $mattermostSender;
+    }
+
+    public function postPersist(LifecycleEventArgs $args)
+    {
+        $entity = $args->getObject();
+        if (!$entity instanceof User) {
+            return;
+        }
+
+        $this->mattermostSender->sendNewUser($entity->getUserIdentifier());
+
+
+    }
+}