浏览代码

Merge branch 'develop' into features/background

François Drouhard 2 年之前
父节点
当前提交
f5290b83c3
共有 4 个文件被更改,包括 26 次插入35 次删除
  1. 5 0
      config/packages/framework.yaml
  2. 2 4
      config/services.yaml
  3. 15 27
      src/Service/Mattermost.php
  4. 4 4
      templates/videotheque/form.html.twig

+ 5 - 0
config/packages/framework.yaml

@@ -27,6 +27,11 @@ framework:
                 headers:
                     Authorization: 'Bearer %env(TMDB_APIKEY_V4)%'
                     Content-Type: 'application/json;charset=utf-8'
+            mattermost.client:
+                base_uri: '%env(resolve:MATTERMOST_URL)%'
+                headers:
+                    Authorization: 'Bearer %env(MATTERMOST_TOKEN)%'
+                    Content-Type: 'application/json;charset=utf-8'
 
 when@test:
     framework:

+ 2 - 4
config/services.yaml

@@ -34,10 +34,8 @@ services:
     
     App\Service\Mattermost:
         arguments:
-            - '%env(MATTERMOST_URL)%'
-            - '%env(MATTERMOST_TOKEN)%'
-            - '%env(MATTERMOST_CHANNEL_ID)%'
-            - '%env(MATTERMOST_CHANNEL_ID_ADMIN)%'
+            $channelId: '%env(MATTERMOST_CHANNEL_ID)%'
+            $channelIdAdmin: '%env(MATTERMOST_CHANNEL_ID_ADMIN)%'
 
     App\Service\FilmCreationListener:
         arguments:

+ 15 - 27
src/Service/Mattermost.php

@@ -7,6 +7,7 @@ use App\Entity\Film;
 use App\Entity\User;
 use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+use Symfony\Contracts\HttpClient\HttpClientInterface;
 
 /**
  * Envoi notifs mattermost
@@ -17,44 +18,31 @@ class Mattermost
      * Mattermost Manager
      */
     public function __construct(
-        protected string $url,
-        protected string $token,
+        protected HttpClientInterface $mattermostClient,
         protected string $channelId,
         protected string $channelIdAdmin,
         protected UrlGeneratorInterface $router,
-        protected RequestStack $requestStack)
+        protected RequestStack $requestStack
+    )
     {
-        $this->url = $url . "/api/v4/posts";
     }
 
-    protected function SendNotif($message, $isChanAdmin = False) :void
+    protected function SendNotif(string $message, bool $isChanAdmin = False) :void
     {
         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 . '"}'
+            $response = $this->mattermostClient->request(
+                'POST',
+                'api/v4/posts', [
+                    'json'  => [
+                        "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->requestStack->getSession()->getFlashBag->add('error', "L'envoi vers mattermost a échoué.");
+            $this->requestStack->getSession()->getFlashBag()->add('error', "L'envoi vers mattermost a échoué.");
         }
-        
     }
 
     public function sendNouveauFilm(Film $film) :void
@@ -86,7 +74,7 @@ class Mattermost
         $this->SendNotif($message);
     }
 
-    public function sendNewUser($userName) : void
+    public function sendNewUser(string $userName) : void
     {
         $message =
         ":warning: Un utilisateur vient de s'enregistrer dans la [vidéothèque]("

+ 4 - 4
templates/videotheque/form.html.twig

@@ -27,10 +27,10 @@
                         <button type="button" id="add_genre" class="btn btn-link add-another-collection-widget mb-3" data-path="{{ path('videotheque_ajax_genres') }}" data-list-selector="#genre-fields-list">Ajouter un genre</button>
                         <ul id="genre-fields-list"
                             data-prototype="{{ form_widget(form.genres.vars.prototype)|e }}"
-                            data-widget-tags="{{ '<li class="list-group-item"></li>'|e }}"
+                            data-widget-tags="{{ '<li class="list-group-item mb-3"></li>'|e }}"
                             data-widget-counter="{{ form.children|length }}">
                             {% for genreField in form.genres %}
-                            <li class="list-group-item">
+                            <li class="list-group-item mb-3">
                                 {{ form_widget(genreField) }}
                                 {{ form_errors(genreField) }}
                             </li>
@@ -51,10 +51,10 @@
                         <button type="button" id="add_realisateur" class="btn btn-link add-another-collection-widget mb-3" data-path="{{ path('videotheque_ajax_realisateurs') }}" data-list-selector="#realisateur-fields-list">Ajouter un réalisateur</button>
                         <ul id="realisateur-fields-list"
                             data-prototype="{{ form_widget(form.realisateurs.vars.prototype)|e }}"
-                            data-widget-tags="{{ '<li class="list-group-item"></li>'|e }}"
+                            data-widget-tags="{{ '<li class="list-group-item mb-3"></li>'|e }}"
                             data-widget-counter="{{ form.children|length }}">
                             {% for realisateurField in form.realisateurs %}
-                            <li class="list-group-item">
+                            <li class="list-group-item mb-3">
                                 {{ form_widget(realisateurField) }}
                                 {{ form_errors(realisateurField) }}
                             </li>