From 24c963ba8b1af69fca967205a5839b7c15ee4099 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Thu, 20 Dec 2012 10:45:05 -0500 Subject: [PATCH] Created abstract base class to simplify redundant code related to Syndetics. --- .../Theme/Root/Helper/AbstractSyndetics.php | 130 ++++++++++++++++++ .../VuFind/Theme/Root/Helper/AuthorNotes.php | 43 +----- .../src/VuFind/Theme/Root/Helper/Excerpt.php | 43 +----- .../src/VuFind/Theme/Root/Helper/Reviews.php | 48 +------ .../VuFind/Theme/Root/Helper/VideoClips.php | 43 +----- 5 files changed, 148 insertions(+), 159 deletions(-) create mode 100644 module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php b/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php new file mode 100644 index 00000000000..204b71843af --- /dev/null +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php @@ -0,0 +1,130 @@ +<?php +/** + * Abstract Syndetics-based view helper. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package View_Helpers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_a_recommendations_module Wiki + */ +namespace VuFind\Theme\Root\Helper; +use Zend\View\Helper\AbstractHelper; + +/** + * Author Notes view helper + * + * @category VuFind2 + * @package View_Helpers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_a_recommendations_module Wiki + */ +abstract class AbstractSyndetics extends AbstractHelper +{ + /** + * VuFind configuration + * + * @var \Zend\Config\Config + */ + protected $config; + + /** + * Object representing ISBN + * + * @var \VuFind\Code\ISBN + */ + protected $isbn; + + /** + * Constructor + */ + public function __construct() + { + $this->config = \VuFind\Config\Reader::getConfig(); + } + + /** + * Store an ISBN; return false if it is invalid. + * + * @param string $isbn ISBN + * + * @return bool + */ + protected function setIsbn($isbn) + { + // We can't proceed without an ISBN: + if (empty($isbn)) { + return false; + } + + $this->isbn = new \VuFind\Code\ISBN($isbn); + return true; + } + + /** + * Attempt to get an ISBN-10; revert to ISBN-13 only when ISBN-10 representation + * is impossible. + * + * @return string + */ + protected function getIsbn10() + { + $isbn = is_object($this->isbn) ? $this->isbn->get10() : false; + if (!$isbn) { + $isbn = $this->isbn->get13(); + } + return $isbn; + } + + /** + * Get an HTTP client + * + * @return \Zend\Http\Client + */ + protected function getHttpClient() + { + return new \VuFind\Http\Client(); + } + + /** + * This method is responsible for retrieving data from Syndetics. + * + * @param string $id Client access key + * @param bool $s_plus Are we operating in Syndetics Plus mode? + * + * @throws \Exception + * @return array + */ + abstract protected function loadSyndetics($id, $s_plus = false); + + /** + * Wrapper around syndetics to provide Syndetics Plus functionality. + * + * @param string $id Client access key + * + * @throws \Exception + * @return array + */ + protected function loadSyndeticsplus($id) + { + return $this->loadSyndetics($id, true); + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php b/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php index f3ef7b83f28..e9f6ae775a8 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php @@ -26,8 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Theme\Root\Helper; -use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, - VuFind\Http\Client as HttpClient, Zend\View\Helper\AbstractHelper; +use DOMDocument; /** * Author Notes view helper @@ -38,11 +37,8 @@ use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ -class AuthorNotes extends AbstractHelper +class AuthorNotes extends AbstractSyndetics { - protected $config; - protected $isbn; - /** * Do the actual work of loading the notes. * @@ -52,13 +48,10 @@ class AuthorNotes extends AbstractHelper */ public function __invoke($isbn) { - // We can't proceed without an ISBN: - if (empty($isbn)) { + if (!$this->setIsbn($isbn)) { return array(); } - $this->config = ConfigReader::getConfig(); - $this->isbn = new ISBN($isbn); $results = array(); // Fetch from provider @@ -86,21 +79,6 @@ class AuthorNotes extends AbstractHelper return $results; } - /** - * Attempt to get an ISBN-10; revert to ISBN-13 only when ISBN-10 representation - * is impossible. - * - * @return string - */ - protected function getIsbn10() - { - $isbn = $this->isbn->get10(); - if (!$isbn) { - $isbn = $this->isbn->get13(); - } - return $isbn; - } - /** * This method is responsible for connecting to Syndetics and abstracting * author notes. @@ -139,7 +117,7 @@ class AuthorNotes extends AbstractHelper $anotes = array(); //find out if there are any notes - $client = new HttpClient(); + $client = $this->getHttpClient(); $client->setUri($url); $result = $client->setMethod('GET')->send(); if (!$result->isSuccess()) { @@ -223,17 +201,4 @@ class AuthorNotes extends AbstractHelper return $anotes; } - - /** - * Wrapper around syndetics to provide Syndetics Plus functionality. - * - * @param string $id Client access key - * - * @throws \Exception - * @return array Returns array with auth notes data. - */ - protected function loadSyndeticsplus($id) - { - return $this->loadSyndetics($id, true); - } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php b/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php index 8096ab20597..fae71bba5bf 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php @@ -26,8 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Theme\Root\Helper; -use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, - VuFind\Http\Client as HttpClient, Zend\View\Helper\AbstractHelper; +use DOMDocument; /** * Excerpt view helper @@ -38,11 +37,8 @@ use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ -class Excerpt extends AbstractHelper +class Excerpt extends AbstractSyndetics { - protected $config; - protected $isbn; - /** * Do the actual work of loading the excerpts. * @@ -52,13 +48,10 @@ class Excerpt extends AbstractHelper */ public function __invoke($isbn) { - // We can't proceed without an ISBN: - if (empty($isbn)) { + if (!$this->setIsbn($isbn)) { return array(); } - $this->config = ConfigReader::getConfig(); - $this->isbn = new ISBN($isbn); $results = array(); // Fetch from provider @@ -86,21 +79,6 @@ class Excerpt extends AbstractHelper return $results; } - /** - * Attempt to get an ISBN-10; revert to ISBN-13 only when ISBN-10 representation - * is impossible. - * - * @return string - */ - protected function getIsbn10() - { - $isbn = $this->isbn->get10(); - if (!$isbn) { - $isbn = $this->isbn->get13(); - } - return $isbn; - } - /** * This method is responsible for connecting to Syndetics and abstracting * excerpts. @@ -140,7 +118,7 @@ class Excerpt extends AbstractHelper $review = array(); //find out if there are any excerpts - $client = new HttpClient(); + $client = $this->getHttpClient(); $client->setUri($url); $result = $client->setMethod('GET')->send(); if (!$result->isSuccess()) { @@ -219,17 +197,4 @@ class Excerpt extends AbstractHelper return $review; } - - /** - * Wrapper around syndetics to provide Syndetics Plus functionality. - * - * @param string $id Client access key - * - * @throws \Exception - * @return array Returns array with auth notes data. - */ - protected function loadSyndeticsplus($id) - { - return $this->loadSyndetics($id, true); - } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php b/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php index 72ce3040731..89f762af30a 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php @@ -26,9 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Theme\Root\Helper; -use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, - VuFind\Http\Client as HttpClient, Zend\View\Helper\AbstractHelper, - ZendService\Amazon\Amazon; +use DOMDocument, ZendService\Amazon\Amazon; /** * Reviews view helper @@ -39,11 +37,8 @@ use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ -class Reviews extends AbstractHelper +class Reviews extends AbstractSyndetics { - protected $config; - protected $isbn; - /** * Do the actual work of loading the reviews. * @@ -53,13 +48,10 @@ class Reviews extends AbstractHelper */ public function __invoke($isbn) { - // We can't proceed without an ISBN: - if (empty($isbn)) { + if (!$this->setIsbn($isbn)) { return array(); } - $this->config = ConfigReader::getConfig(); - $this->isbn = new ISBN($isbn); $results = array(); // Fetch from provider @@ -87,21 +79,6 @@ class Reviews extends AbstractHelper return $results; } - /** - * Attempt to get an ISBN-10; revert to ISBN-13 only when ISBN-10 representation - * is impossible. - * - * @return string - */ - protected function getIsbn10() - { - $isbn = $this->isbn->get10(); - if (!$isbn) { - $isbn = $this->isbn->get13(); - } - return $isbn; - } - /** * Amazon Reviews * @@ -152,7 +129,7 @@ class Reviews extends AbstractHelper $url = 'http://' . $endpoint . $requestURI . '?' . $encodedParams . '&Signature=' . rawurlencode(base64_encode($hmacHash)); - $client = new HttpClient(); + $client = $this->getHttpClient(); $client->setUri($url); $result = $client->setMethod('GET')->send(); @@ -315,7 +292,7 @@ class Reviews extends AbstractHelper $review = array(); //find out if there are any reviews - $client = new HttpClient(); + $client = $this->getHttpClient(); $client->setUri($url); $result = $client->setMethod('GET')->send(); if (!$result->isSuccess()) { @@ -396,19 +373,6 @@ class Reviews extends AbstractHelper return $review; } - /** - * Wrapper around syndetics to provide Syndetics Plus functionality. - * - * @param string $id Client access key - * - * @throws \Exception - * @return array Returns array with auth notes data. - */ - protected function loadSyndeticsplus($id) - { - return $this->loadSyndetics($id, true); - } - /** * Guardian Reviews * @@ -435,7 +399,7 @@ class Reviews extends AbstractHelper } //find out if there are any reviews - $client = new HttpClient(); + $client = $this->getHttpClient(); $client->setUri($url); $result = $client->setMethod('GET')->send(); diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php b/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php index ba77d19a8fe..2900f928830 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php @@ -26,8 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Theme\Root\Helper; -use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, - VuFind\Http\Client as HttpClient, Zend\View\Helper\AbstractHelper; +use DOMDocument; /** * Video clip view helper @@ -38,11 +37,8 @@ use DOMDocument, VuFind\Config\Reader as ConfigReader, VuFind\Code\ISBN, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ -class VideoClips extends AbstractHelper +class VideoClips extends AbstractSyndetics { - protected $config; - protected $isbn; - /** * Do the actual work of loading the clips. * @@ -52,13 +48,10 @@ class VideoClips extends AbstractHelper */ public function __invoke($isbn) { - // We can't proceed without an ISBN: - if (empty($isbn)) { + if (!$this->setIsbn($isbn)) { return array(); } - $this->config = ConfigReader::getConfig(); - $this->isbn = new ISBN($isbn); $results = array(); // Fetch from provider @@ -86,21 +79,6 @@ class VideoClips extends AbstractHelper return $results; } - /** - * Attempt to get an ISBN-10; revert to ISBN-13 only when ISBN-10 representation - * is impossible. - * - * @return string - */ - protected function getIsbn10() - { - $isbn = $this->isbn->get10(); - if (!$isbn) { - $isbn = $this->isbn->get13(); - } - return $isbn; - } - /** * This method is responsible for connecting to Syndetics and abstracting * clips. @@ -139,7 +117,7 @@ class VideoClips extends AbstractHelper $vclips = array(); //find out if there are any clips - $client = new HttpClient(); + $client = $this->getHttpClient(); $client->setUri($url); $result = $client->setMethod('GET')->send(); if (!$result->isSuccess()) { @@ -213,17 +191,4 @@ class VideoClips extends AbstractHelper return $vclips; } - - /** - * Wrapper around syndetics to provide Syndetics Plus functionality. - * - * @param string $id Client access key - * - * @throws \Exception - * @return array Returns array with auth notes data. - */ - protected function loadSyndeticsplus($id) - { - return $this->loadSyndetics($id, true); - } } \ No newline at end of file -- GitLab