From f1bcb5c2f5e68b6f95adffd67f28999d66445192 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 16 Jan 2013 09:48:29 -0500
Subject: [PATCH] More refactoring to simplify Syndetics logic.

---
 .../View/Helper/Root/AbstractSyndetics.php    | 22 +++++++++++++++----
 .../VuFind/View/Helper/Root/AuthorNotes.php   | 14 ++++--------
 .../src/VuFind/View/Helper/Root/Excerpt.php   | 16 +++++---------
 .../src/VuFind/View/Helper/Root/Reviews.php   | 15 +++++--------
 .../VuFind/View/Helper/Root/VideoClips.php    | 14 ++++--------
 5 files changed, 36 insertions(+), 45 deletions(-)

diff --git a/module/VuFind/src/VuFind/View/Helper/Root/AbstractSyndetics.php b/module/VuFind/src/VuFind/View/Helper/Root/AbstractSyndetics.php
index 093ef97468e..47cdff5caac 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/AbstractSyndetics.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/AbstractSyndetics.php
@@ -87,6 +87,23 @@ abstract class AbstractSyndetics extends AbstractHelper
         return true;
     }
 
+    /**
+     * Get the Syndetics URL for making a request.
+     *
+     * @param string $id   Client ID
+     * @param string $file File to request
+     * @param string $type Type parameter
+     *
+     * @return string
+     */
+    protected function getSyndeticsUrl($id, $file = 'index.xml', $type = 'rw12,h7')
+    {
+        $baseUrl = isset($this->config->Syndetics->url)
+            ? $this->config->Syndetics->url : 'http://syndetics.com';
+        return $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10()
+            . '/' . $file . '&client=' . $id . '&type=' . $type;
+    }
+
     /**
      * Attempt to get an ISBN-10; revert to ISBN-13 only when ISBN-10 representation
      * is impossible.
@@ -96,10 +113,7 @@ abstract class AbstractSyndetics extends AbstractHelper
     protected function getIsbn10()
     {
         $isbn = is_object($this->isbn) ? $this->isbn->get10() : false;
-        if (!$isbn) {
-            $isbn = $this->isbn->get13();
-        }
-        return $isbn;
+        return (!$isbn && is_object($this->isbn)) ? $this->isbn->get13() : $isbn;
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/AuthorNotes.php b/module/VuFind/src/VuFind/View/Helper/Root/AuthorNotes.php
index 3d35e0bca47..83d43f20ef0 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/AuthorNotes.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/AuthorNotes.php
@@ -82,15 +82,11 @@ class AuthorNotes extends AbstractSyndetics
             )
         );
 
-        //first request url
-        $baseUrl = isset($this->config->Syndetics->url) ?
-            $this->config->Syndetics->url : 'http://syndetics.com';
-        $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() .
-               '/index.xml&client=' . $id . '&type=rw12,hw7';
-
+        // Initialize return value
         $anotes = array();
 
-        //find out if there are any notes
+        // Find out if there are any notes
+        $url = $this->getSyndeticsUrl($id);
         $result = $this->getHttpClient($url)->send();
         if (!$result->isSuccess()) {
             return $anotes;
@@ -106,9 +102,7 @@ class AuthorNotes extends AbstractSyndetics
             $nodes = $xmldoc->getElementsByTagName($source);
             if ($nodes->length) {
                 // Load notes
-                $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() . '/' .
-                       $sourceInfo['file'] . '&client=' . $id . '&type=rw12,hw7';
-
+                $url = $this->getSyndeticsUrl($id, $sourceInfo['file']);
                 $result2 = $this->getHttpClient($url)->send();
                 if (!$result2->isSuccess()) {
                     continue;
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Excerpt.php b/module/VuFind/src/VuFind/View/Helper/Root/Excerpt.php
index 412e4bfe6ea..b9665ebe10b 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Excerpt.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Excerpt.php
@@ -74,7 +74,7 @@ class Excerpt extends AbstractSyndetics
      */
     protected function loadSyndetics($id, $s_plus=false)
     {
-        //list of syndetic excerpts
+        // List of syndetic excerpts
         $sourceList = array(
             'DBCHAPTER' => array(
                 'title' => 'First Chapter or Excerpt',
@@ -83,15 +83,11 @@ class Excerpt extends AbstractSyndetics
             )
         );
 
-        //first request url
-        $baseUrl = isset($this->config->Syndetics->url) ?
-            $this->config->Syndetics->url : 'http://syndetics.com';
-        $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() .
-               '/index.xml&client=' . $id . '&type=rw12,hw7';
-
+        // Initialize return value:
         $review = array();
 
-        //find out if there are any excerpts
+        // Find out if there are any excerpts
+        $url = $this->getSyndeticsUrl($id);
         $result = $this->getHttpClient($url)->send();
         if (!$result->isSuccess()) {
             return $review;
@@ -107,9 +103,7 @@ class Excerpt extends AbstractSyndetics
             $nodes = $xmldoc->getElementsByTagName($source);
             if ($nodes->length) {
                 // Load excerpts
-                $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() . '/' .
-                       $sourceInfo['file'] . '&client=' . $id . '&type=rw12,hw7';
-
+                $url = $this->getSyndeticsUrl($id, $sourceInfo['file']);
                 $result2 = $this->getHttpClient($url)->send();
                 if (!$result2->isSuccess()) {
                     continue;
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Reviews.php b/module/VuFind/src/VuFind/View/Helper/Root/Reviews.php
index 5d36d67217b..cb0aa37452a 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Reviews.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Reviews.php
@@ -218,7 +218,7 @@ class Reviews extends AbstractSyndetics
      */
     protected function loadSyndetics($id, $s_plus=false)
     {
-        //list of syndetic reviews
+        // List of syndetic reviews
         $sourceList = array(
             'CHREVIEW' => array('title' => 'Choice Review',
                                 'file' => 'CHREVIEW.XML',
@@ -255,15 +255,11 @@ class Reviews extends AbstractSyndetics
                                 'file' => 'CRITICASEREVIEW.XML')
         );
 
-        //first request url
-        $baseUrl = isset($this->config->Syndetics->url) ?
-            $this->config->Syndetics->url : 'http://syndetics.com';
-        $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() . '/' .
-               'index.xml&client=' . $id . '&type=rw12,hw7';
-
+        // Initialize return value
         $review = array();
 
-        //find out if there are any reviews
+        // Find out if there are any reviews
+        $url = $this->getSyndeticsUrl($id);
         $result = $this->getHttpClient($url)->send();
         if (!$result->isSuccess()) {
             return $review;
@@ -279,8 +275,7 @@ class Reviews extends AbstractSyndetics
             $nodes = $xmldoc->getElementsByTagName($source);
             if ($nodes->length) {
                 // Load reviews
-                $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() . '/' .
-                       $sourceInfo['file'] . '&client=' . $id . '&type=rw12,hw7';
+                $url = $this->getSyndeticsUrl($id, $sourceInfo['file']);
                 $result2 = $this->getHttpClient($url)->send();
                 if (!$result2->isSuccess()) {
                     continue;
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/VideoClips.php b/module/VuFind/src/VuFind/View/Helper/Root/VideoClips.php
index 2ab67a091a3..884a70709c6 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/VideoClips.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/VideoClips.php
@@ -82,15 +82,11 @@ class VideoClips extends AbstractSyndetics
             )
         );
 
-        //first request url
-        $baseUrl = isset($this->config->Syndetics->url) ?
-            $this->config->Syndetics->url : 'http://syndetics.com';
-        $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() .
-               '/index.xml&client=' . $id . '&type=rw12,hw7';
-
+        // Initialize return value:
         $vclips = array();
 
-        //find out if there are any clips
+        // Find out if there are any clips
+        $url = $this->getSyndeticsUrl($id);
         $result = $this->getHttpClient($url)->send();
         if (!$result->isSuccess()) {
             return $vclips;
@@ -106,9 +102,7 @@ class VideoClips extends AbstractSyndetics
             $nodes = $xmldoc->getElementsByTagName($source);
             if ($nodes->length) {
                 // Load clips
-                $url = $baseUrl . '/index.aspx?isbn=' . $this->getIsbn10() . '/' .
-                       $sourceInfo['file'] . '&client=' . $id . '&type=rw12,hw7';
-
+                $url = $this->getSyndeticsUrl($id, $sourceInfo['file']);
                 $result2 = $this->getHttpClient($url)->send();
                 if (!$result2->isSuccess()) {
                     continue;
-- 
GitLab