From 01f47ad37427ddb9b1ee5b8966dc9f1234dba095 Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.net>
Date: Fri, 18 Aug 2023 10:37:50 +0000
Subject: [PATCH] hotline#183834 : ArteVOD : Better failure management for Film
 Reader

---
 VERSIONS_HOTLINE/183834                       |  1 +
 .../ArteVod/Service/Film.php                  | 21 ++++++------
 .../ArteVod/Service/FilmsReader.php           | 12 +++++--
 .../ArteVod/tests/ArteVodFixtures.php         | 12 ++++++-
 .../ArteVod/tests/ArteVodTest.php             | 32 +++++++++++++++++++
 5 files changed, 63 insertions(+), 15 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/183834

diff --git a/VERSIONS_HOTLINE/183834 b/VERSIONS_HOTLINE/183834
new file mode 100644
index 00000000000..b8b0521a0ed
--- /dev/null
+++ b/VERSIONS_HOTLINE/183834
@@ -0,0 +1 @@
+ - correctif #183834 : ArteVOD : Meilleure gestion d'erreurs dans les données exposées par ArteVOD
\ No newline at end of file
diff --git a/library/digital_resources/ArteVod/Service/Film.php b/library/digital_resources/ArteVod/Service/Film.php
index 1c74cfbfd3c..a9b9586e025 100644
--- a/library/digital_resources/ArteVod/Service/Film.php
+++ b/library/digital_resources/ArteVod/Service/Film.php
@@ -31,11 +31,10 @@ class ArteVod_Service_Film
     $this->setId($json['id'])
          ->setTitle($json['title'])
          ->setDescription($json['description'])
-         ->addActors(isset($json['actors']) ?
-                     $json['actors'] : [])
+         ->addActors($json['actors'] ?? [])
          ->setDuration($json['duration'])
-         ->addMedias(isset($json['medias']) ? $json['medias'] : [])
-         ->addThemes($json['themes']);
+         ->addMedias($json['medias'] ?? [])
+         ->addThemes($json['themes'] ?? []);
 
     if (isset($json['genres']))
       $this->setGenres($json['genres']);
@@ -55,19 +54,19 @@ class ArteVod_Service_Film
 
   protected function _setCustomFields($json) {
     $this->addPoster($json['posterUrl'])
-         ->setDirectors($json['directors'])
+         ->setDirectors($json['directors'] ?? [])
          ->setExternalUri($json['url']);
     return $this;
   }
 
 
-  protected function setDirectors($directors) {
+  protected function setDirectors(array $directors) {
     $this->_directors = $directors;
     return $this;
   }
 
 
-  protected function addMedias($medias) {
+  protected function addMedias(array $medias) {
     array_map([$this,'addMedia'],
               $medias);
     return $this;
@@ -84,14 +83,14 @@ class ArteVod_Service_Film
   }
 
 
-  protected function addThemes($themes) {
+  protected function addThemes(array $themes) {
     array_map([$this, 'addTag'],
               $themes);
     return $this;
   }
 
 
-  protected function setGenres($genre) {
+  protected function setGenres(array $genre) {
     array_map([$this, 'addMatiere'],
               $genre);
     return $this;
@@ -105,7 +104,7 @@ class ArteVod_Service_Film
   public function fillAlbum(Class_Album $album) : self {
     $this->setTypeDoc($album)
          ->setDuration($this->_duration.' mn')
-         ->setTags($this->getTags());
+         ->setTags($this->getTags() ?? []);
 
     foreach ($this->_directors as $director)
       $this->_fillDirectorIn($album, $director);
@@ -131,7 +130,7 @@ class ArteVod_Service_Film
   }
 
 
-  public function addActors($actors) {
+  public function addActors(array $actors) {
     $this->_actors = $actors;
     return $this;
   }
diff --git a/library/digital_resources/ArteVod/Service/FilmsReader.php b/library/digital_resources/ArteVod/Service/FilmsReader.php
index aec811e5008..59a9b603019 100644
--- a/library/digital_resources/ArteVod/Service/FilmsReader.php
+++ b/library/digital_resources/ArteVod/Service/FilmsReader.php
@@ -20,11 +20,17 @@
  */
 
 class ArteVod_Service_FilmsReader {
-  protected $_films;
+  protected array $_films = [];
+
+  public function parse(string $json) {
+    if (FALSE !== strpos($json,'"content":null'))
+      return $this;
+
+    if (! is_array($objects = json_decode($json, true)))
+      return $this;
 
-  public function parse($json) {
     $this->_films = array_map([$this, 'parseFilm'],
-                              json_decode($json, true));
+                              $objects);
     return $this;
   }
 
diff --git a/library/digital_resources/ArteVod/tests/ArteVodFixtures.php b/library/digital_resources/ArteVod/tests/ArteVodFixtures.php
index e49ee65a8f3..33e3560c2f7 100644
--- a/library/digital_resources/ArteVod/tests/ArteVodFixtures.php
+++ b/library/digital_resources/ArteVod/tests/ArteVodFixtures.php
@@ -30,11 +30,21 @@ class ArteVodFixtures {
 
 
   public static function secondPage() {
-    return '[{"id":7002,"title":"Manon 20 ans","description":"<p>Envoyée en centre éducatif fermé après avoir poignardé sa mère, Manon a six mois pour faire ses preuves. Une minisérie à la puissance émotionnelle bouleversante, récompensée du Fipa d&#39;or, avec Alba Gaïa Bellugi et Marina Foïs.</p>","themes":[],"productionYear":null,"posterUrl":"https://vod.mediatheque-numerique.com/media/59/3a/593a5d947c887.jpeg","trailerUrl":null,"url":"https://vod.mediatheque-numerique.com/films/manon-20-ans","duration":10080,"audioLanguages":[],"directors":[],"actors":[],"publicationDate":"2017-06-09","genres":["Cinéma","Série TV"],"productionCountry":null,"codes":[],"medias":[{"type":"POSTER","url":"https://vod.mediatheque-numerique.com/media/59/3a/593a5d947c887.jpeg","modificationDate":"2017-06-09T10:34:28"}],"rate":null,"comments":[],"commentsLibrary":[],"bonus":[],"availableCountries":[{"type":"Country","code":"BE"},{"type":"Country","code":"BG"},{"type":"Country","code":"CH"},{"type":"Country","code":"CZ"},{"type":"Country","code":"CY"},{"type":"Country","code":"RO"},{"type":"Country","code":"GR"},{"type":"Country","code":"SK"},{"type":"Country","code":"SI"},{"type":"Country","code":"SE"},{"type":"Country","code":"FR"},{"type":"Country","code":"DK"},{"type":"Country","code":"HU"},{"type":"Country","code":"LT"},{"type":"Country","code":"PT"},{"type":"Country","code":"TR"},{"type":"Country","code":"LI"},{"type":"Country","code":"LV"},{"type":"Country","code":"PL"},{"type":"Country","code":"LU"},{"type":"Country","code":"EE"},{"type":"Country","code":"DE"},{"type":"Country","code":"IT"},{"type":"Country","code":"ES"},{"type":"Country","code":"HR"},{"type":"Country","code":"IS"},{"type":"Country","code":"AT"},{"type":"Country","code":"MT"},{"type":"Country","code":"IE"},{"type":"Country","code":"UK"},{"type":"Country","code":"FI"},{"type":"Country","code":"NL"},{"type":"Country","code":"NO"}],"targetAudiences":[]},{"id":7009,"title":"Squadra Criminale","description":"<p>Douze meurtres. Douze histoires de crimes...<br />\r\nCapitaine à la brigade criminelle de Turin, Valeria Ferro est une enquêtrice aussi tenace que talentueuse. Elle semble posséder un sixième sens pour reconstituer le puzzle complexe de chaque affaire tout en luttant contre les fantômes de son passé. Ceux-ci resurgissent violemment dans sa vie au moment où sa mère est libérée de prison.</p>","themes":[],"productionYear":null,"posterUrl":"https://vod.mediatheque-numerique.com/media/59/3a/593a6efba55d1.jpeg","trailerUrl":"https://media.universcine.com/cd/b5/cdb57adc-2a49-11e7-b234-bdefb096dc40.mp4","url":"https://vod.mediatheque-numerique.com/films/squadra-criminale","duration":35400,"audioLanguages":[],"directors":[],"actors":[],"publicationDate":"2017-06-09","genres":["Série TV"],"productionCountry":null,"codes":[],"medias":[{"type":"POSTER","url":"https://vod.mediatheque-numerique.com/media/59/3a/593a6efba55d1.jpeg","modificationDate":"2017-06-09T11:48:43"}],"rate":null,"comments":[],"commentsLibrary":[],"bonus":[],"availableCountries":[{"type":"Country","code":"NC"},{"type":"Country","code":"BE"},{"type":"Country","code":"PF"},{"type":"Country","code":"GP"},{"type":"Country","code":"WF"},{"type":"Country","code":"GF"},{"type":"Country","code":"CH"},{"type":"Country","code":"PM"},{"type":"Country","code":"YT"},{"type":"Country","code":"FR"},{"type":"Country","code":"MF"},{"type":"Country","code":"AD"},{"type":"Country","code":"RE"},{"type":"Country","code":"MQ"},{"type":"Country","code":"MC"}],"targetAudiences":[]}]';
+    return '[{"id":7002,"title":"Manon 20 ans","description":"<p>Envoyée en centre éducatif fermé après avoir poignardé sa mère, Manon a six mois pour faire ses preuves. Une minisérie à la puissance émotionnelle bouleversante, récompensée du Fipa d&#39;or, avec Alba Gaïa Bellugi et Marina Foïs.</p>","themes":[],"productionYear":null,"posterUrl":"https://vod.mediatheque-numerique.com/media/59/3a/593a5d947c887.jpeg","trailerUrl":null,"url":"https://vod.mediatheque-numerique.com/films/manon-20-ans","duration":10080,"audioLanguages":[],"actors":[],"publicationDate":"2017-06-09","genres":["Cinéma","Série TV"],"productionCountry":null,"codes":[],"medias":[{"type":"POSTER","url":"https://vod.mediatheque-numerique.com/media/59/3a/593a5d947c887.jpeg","modificationDate":"2017-06-09T10:34:28"}],"rate":null,"comments":[],"commentsLibrary":[],"bonus":[],"availableCountries":[{"type":"Country","code":"BE"},{"type":"Country","code":"BG"},{"type":"Country","code":"CH"},{"type":"Country","code":"CZ"},{"type":"Country","code":"CY"},{"type":"Country","code":"RO"},{"type":"Country","code":"GR"},{"type":"Country","code":"SK"},{"type":"Country","code":"SI"},{"type":"Country","code":"SE"},{"type":"Country","code":"FR"},{"type":"Country","code":"DK"},{"type":"Country","code":"HU"},{"type":"Country","code":"LT"},{"type":"Country","code":"PT"},{"type":"Country","code":"TR"},{"type":"Country","code":"LI"},{"type":"Country","code":"LV"},{"type":"Country","code":"PL"},{"type":"Country","code":"LU"},{"type":"Country","code":"EE"},{"type":"Country","code":"DE"},{"type":"Country","code":"IT"},{"type":"Country","code":"ES"},{"type":"Country","code":"HR"},{"type":"Country","code":"IS"},{"type":"Country","code":"AT"},{"type":"Country","code":"MT"},{"type":"Country","code":"IE"},{"type":"Country","code":"UK"},{"type":"Country","code":"FI"},{"type":"Country","code":"NL"},{"type":"Country","code":"NO"}],"targetAudiences":[]},{"id":7009,"title":"Squadra Criminale","description":"<p>Douze meurtres. Douze histoires de crimes...<br />\r\nCapitaine à la brigade criminelle de Turin, Valeria Ferro est une enquêtrice aussi tenace que talentueuse. Elle semble posséder un sixième sens pour reconstituer le puzzle complexe de chaque affaire tout en luttant contre les fantômes de son passé. Ceux-ci resurgissent violemment dans sa vie au moment où sa mère est libérée de prison.</p>","productionYear":null,"posterUrl":"https://vod.mediatheque-numerique.com/media/59/3a/593a6efba55d1.jpeg","trailerUrl":"https://media.universcine.com/cd/b5/cdb57adc-2a49-11e7-b234-bdefb096dc40.mp4","url":"https://vod.mediatheque-numerique.com/films/squadra-criminale","duration":35400,"audioLanguages":[],"publicationDate":"2017-06-09","genres":["Série TV"],"productionCountry":null,"codes":[],"medias":[{"type":"POSTER","url":"https://vod.mediatheque-numerique.com/media/59/3a/593a6efba55d1.jpeg","modificationDate":"2017-06-09T11:48:43"}],"rate":null,"comments":[],"commentsLibrary":[],"bonus":[],"availableCountries":[{"type":"Country","code":"NC"},{"type":"Country","code":"BE"},{"type":"Country","code":"PF"},{"type":"Country","code":"GP"},{"type":"Country","code":"WF"},{"type":"Country","code":"GF"},{"type":"Country","code":"CH"},{"type":"Country","code":"PM"},{"type":"Country","code":"YT"},{"type":"Country","code":"FR"},{"type":"Country","code":"MF"},{"type":"Country","code":"AD"},{"type":"Country","code":"RE"},{"type":"Country","code":"MQ"},{"type":"Country","code":"MC"}],"targetAudiences":[]}]';
   }
 
 
   public static function emptyPage() {
     return '[]';
   }
+
+
+  public static function nullContentPage() {
+    return '{"content":null}';
+  }
+
+
+  public static function brokenJsonPage() {
+    return '{"content":null';
+  }
 }
diff --git a/library/digital_resources/ArteVod/tests/ArteVodTest.php b/library/digital_resources/ArteVod/tests/ArteVodTest.php
index 2b796e979a1..4be3ee29f74 100644
--- a/library/digital_resources/ArteVod/tests/ArteVodTest.php
+++ b/library/digital_resources/ArteVod/tests/ArteVodTest.php
@@ -421,3 +421,35 @@ class ArteVodWebClientTest extends ModelTestCase {
                            Class_WebService_WaitingSimpleWebClient::class));
   }
 }
+
+
+
+
+class ArteVodHarverstingNullContentPageTest extends ArteVodHarverstingTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    foreach(['1' => ArteVodFixtures::nullContentPage(),
+             '2' => ArteVodFixtures::brokenJsonPage(),
+             '3' => ArteVodFixtures::emptyPage()]
+            as $page => $answer) {
+
+      $this->_web_client
+        ->whenCalled('open_url')
+        ->with('https://mednumv3-backapi.lab.arte.tv/api/v1/films?page_nb=' . $page . '&q_country=FR')
+        ->answers($answer);
+    }
+
+    $this->_web_client
+      ->whenCalled('setConfig')->with(['timeout' => 10])->answers($this->_web_client)
+      ->beStrict();
+
+    $this->_service->harvest();
+  }
+
+
+  /** @test */
+  public function shouldHaveDeletedAllAlbum() {
+    $this->assertEquals(0, count(Class_Album::findAll()));
+  }
+}
-- 
GitLab