From 98e56c5640e660e8ae5aadd981853656543a1e35 Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Tue, 16 Oct 2018 18:06:53 +0200
Subject: [PATCH] hotline #80421 : extract thumbnail related methods from
 record model

---
 library/Class/Notice.php           | 56 ++-------------------
 library/Class/Notice/Thumbnail.php | 79 ++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 53 deletions(-)
 create mode 100644 library/Class/Notice/Thumbnail.php

diff --git a/library/Class/Notice.php b/library/Class/Notice.php
index c900c1c308b..572cee73fbf 100644
--- a/library/Class/Notice.php
+++ b/library/Class/Notice.php
@@ -405,45 +405,12 @@ class Class_Notice extends Storm_Model_Abstract {
 
 
   public function fetchUrlVignette() {
-    $service = new Class_WebService_Vignette();
-
-    if ($this->shouldRetryUrlVignette())
-      return $service->getAjaxUrl($this);
-
-    $url = $this->getUrlVignette();
-
-    return $service->isNoData($url)
-      ? $this->_rawThumbnailUrl()
-      : $url;
+    return (new Class_Notice_Thumbnail())->fetchUrlVignette($this);
   }
 
 
   public function fetchUrlLocalVignette() {
-    $service = new Class_WebService_Vignette();
-
-    if ($this->shouldRetryUrlVignette())
-      $service->updateUrlsFromCacheServer($this);
-
-    $url = $this->getUrlVignette();
-
-    return $service->isNoData($url)
-      ? $this->_rawThumbnailUrl()
-      : $service->writeImageCache($this, $url);
-  }
-
-
-  public function shouldRetryUrlVignette() {
-    $service = new Class_WebService_Vignette();
-    return !$this->getUrlVignette()
-      || ($service->isNoData($this->getUrlVignette())
-          && $service->isThirdParty($this));
-  }
-
-
-  protected function _rawThumbnailUrl() {
-    return Class_Url::assemble(['controller' => 'recherche',
-                                'action' => 'raw-thumbnail',
-                                'id' => $this->getId()]);
+    return (new Class_Notice_Thumbnail())->fetchUrlLocalVignette($this);
   }
 
 
@@ -453,24 +420,7 @@ class Class_Notice extends Storm_Model_Abstract {
 
 
   public function fetchUrlImage() {
-    $service = new Class_WebService_Vignette();
-
-    if ($this->shouldRetryUrlImage())
-      $service->updateUrlsFromCacheServer($this);
-
-    $url = $this->getUrlImage();
-
-    return $service->isNoData($url)
-      ? $this->fetchUrlLocalVignette()
-      : $url;
-  }
-
-
-  public function shouldRetryUrlImage() {
-    $service = new Class_WebService_Vignette();
-    return !$this->getUrlImage()
-      || ($service->isNoData($this->getUrlImage())
-          && $service->isThirdParty($this));
+    return (new Class_Notice_Thumbnail())->fetchUrlImage($this);
   }
 
 
diff --git a/library/Class/Notice/Thumbnail.php b/library/Class/Notice/Thumbnail.php
new file mode 100644
index 00000000000..20643eedcd7
--- /dev/null
+++ b/library/Class/Notice/Thumbnail.php
@@ -0,0 +1,79 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Notice_Thumbnail {
+  protected $_service;
+
+  public function __construct() {
+    $this->_service = new Class_WebService_Vignette();
+  }
+
+
+  public function fetchUrlVignette($record) {
+    if ($this->_shouldRetry($record, $record->getUrlVignette()))
+      return $this->_service->getAjaxUrl($record);
+
+    $url = $record->getUrlVignette();
+
+    return $this->_service->isNoData($url)
+      ? $this->_rawThumbnailUrl($record)
+      : $url;
+  }
+
+
+  public function fetchUrlLocalVignette($record) {
+    if ($this->_shouldRetry($record, $record->getUrlVignette()))
+      $this->_service->updateUrlsFromCacheServer($record);
+
+    $url = $record->getUrlVignette();
+
+    return $this->_service->isNoData($url)
+      ? $this->_rawThumbnailUrl($record)
+      : $this->_service->writeImageCache($record, $url);
+  }
+
+
+  public function fetchUrlImage($record) {
+    if ($this->_shouldRetry($record, $record->getUrlImage()))
+      $this->_service->updateUrlsFromCacheServer($record);
+
+    $url = $record->getUrlImage();
+
+    return $this->_service->isNoData($url)
+      ? $this->fetchUrlLocalVignette($record)
+      : $url;
+  }
+
+
+  protected function _rawThumbnailUrl($record) {
+    return Class_Url::assemble(['controller' => 'recherche',
+                                'action' => 'raw-thumbnail',
+                                'id' => $record->getId()]);
+  }
+
+
+  protected function _shouldRetry($record, $url) {
+    return !$url
+      || ($this->_service->isNoData($url)
+          && $this->_service->isThirdParty($record));
+  }
+}
-- 
GitLab