Browse Source

Ajout d'un test pour envoi Mattermost

François Drouhard 2 năm trước cách đây
mục cha
commit
c6c13b247d
2 tập tin đã thay đổi với 45 bổ sung33 xóa
  1. 31 19
      src/Service/Mattermost.php
  2. 14 14
      templates/flashbag.html.twig

+ 31 - 19
src/Service/Mattermost.php

@@ -5,7 +5,8 @@ namespace App\Service;
 
 use App\Entity\Film;
 use App\Entity\User;
-use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;;
 
 /**
  * Envoi notifs mattermost
@@ -17,38 +18,49 @@ class Mattermost
     protected $channelId;
     protected $channelIdAdmin;
     protected $router;
+    protected $flashBagInterface;
 
     /**
      * Mattermost Manager
      */
-    public function __construct($url, $token, $channelId, $channelIdAdmin, UrlGeneratorInterface $router)
+    public function __construct($url, $token, $channelId, $channelIdAdmin, UrlGeneratorInterface $router, FlashBagInterface $flashBagInterface)
     {
         $this->url = $url . "/api/v4/posts";
         $this->token = $token;
         $this->channelId = $channelId;
         $this->channelIdAdmin = $channelIdAdmin;
         $this->router = $router;
+        $this->flashBagInterface = $flashBagInterface;
     }
 
     protected function SendNotif($message, $isChanAdmin = False) :void
     {
-        // crée une nouvelle ressource cURL
-        $ch = curl_init();
-
-        // fixe l'URL et les autres options appropriées
-        $options = array(
-            CURLOPT_URL => $this->url,
-            CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Authorization: Bearer ' . $this->token),
-            CURLOPT_POSTFIELDS  =>  '{"channel_id": "' . ($isChanAdmin ? $this->channelIdAdmin : $this->channelId) . '", "message": "' . $message . '"}'
-        );
-
-        curl_setopt_array($ch, $options);
-
-        // attrape l'URL et la passe au navigateur
-        curl_exec($ch);
-
-        // ferme la ressource cURL et libère les ressources systèmes
-        curl_close($ch);
+        try {
+            if (function_exists("curl_init")) {
+            // crée une nouvelle ressource cURL
+                $ch = curl_init();
+            } else {
+                throw new \Exception("Curl n'est pas installé.", 500);
+            }
+
+            // fixe l'URL et les autres options appropriées
+            $options = array(
+                CURLOPT_URL => $this->url,
+                CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Authorization: Bearer ' . $this->token),
+                CURLOPT_POSTFIELDS  =>  '{"channel_id": "' . ($isChanAdmin ? $this->channelIdAdmin : $this->channelId) . '", "message": "' . $message . '"}'
+            );
+
+            curl_setopt_array($ch, $options);
+
+            // attrape l'URL et la passe au navigateur
+            curl_exec($ch);
+
+            // ferme la ressource cURL et libère les ressources systèmes
+            curl_close($ch);
+        } catch (\Exception $e) {
+            $this->flashBagInterface->add('error', "L'envoi vers mattermost a échoué.");
+        }
+        
     }
 
     public function sendNouveauFilm(Film $film) :void

+ 14 - 14
templates/flashbag.html.twig

@@ -1,16 +1,16 @@
-{% for type, messages in app.session.flashBag.all %}
-    {% for message in messages %}
-        {% if type == 'error' %}{% set type = 'danger' %} {% endif %}
-        <div class="position-fixed top-5 end-0 p-2" style="z-index: 11">
-            <div id="liveToast" class="toast bg-gradient bg-{{ type }} text-white" role="alert" aria-live="assertive" aria-atomic="true" {{ stimulus_controller('toast') }}>
-                <div class="toast-header">
-                <strong class="me-auto">Info</strong>
-                <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
+<div class="position-fixed top-5 end-0 p-2" style="z-index: 11">
+    {% for type, messages in app.session.flashBag.all %}
+        {% for message in messages %}
+            {% if type == 'error' %}{% set type = 'danger' %} {% endif %}
+                <div id="liveToast" class="toast bg-gradient bg-{{ type }} text-white" role="alert" aria-live="assertive" aria-atomic="true" {{ stimulus_controller('toast') }}>
+                    <div class="toast-header">
+                    <strong class="me-auto">Info</strong>
+                    <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
+                    </div>
+                    <div class="toast-body">
+                        {{ message }}
+                    </div>
                 </div>
-                <div class="toast-body">
-                    {{ message }}
-                </div>
-            </div>
-        </div>
+        {% endfor %}
     {% endfor %}
-{% endfor %}
+</div>