From 6767fc78b4c8184b66e4eb795d5bc79c88690272 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Thu, 20 Dec 2012 11:42:45 -0500 Subject: [PATCH] Eliminated more redundant code. --- .../Theme/Root/Helper/AbstractSyndetics.php | 38 +++++++++++++++++++ .../VuFind/Theme/Root/Helper/AuthorNotes.php | 32 ++-------------- .../src/VuFind/Theme/Root/Helper/Excerpt.php | 32 ++-------------- .../src/VuFind/Theme/Root/Helper/Reviews.php | 32 ++-------------- .../VuFind/Theme/Root/Helper/VideoClips.php | 32 ++-------------- 5 files changed, 50 insertions(+), 116 deletions(-) diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php b/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php index a9dbcfa1d98..83b31de9fc1 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/AbstractSyndetics.php @@ -102,6 +102,44 @@ abstract class AbstractSyndetics extends AbstractHelper return $isbn; } + /** + * Retrieve results for the providers specified. + * + * @param string $isbn ISBN to use for lookup + * @param string $providers Provider configuration + * + * @return array + */ + protected function getResults($isbn, $providers) + { + $results = array(); + if (!$this->setIsbn($isbn)) { + return $results; + } + + // Fetch from provider + $providers = explode(',', $providers); + foreach ($providers as $provider) { + $parts = explode(':', trim($provider)); + $provider = strtolower($parts[0]); + $func = 'load' . ucwords($provider); + $key = $parts[1]; + try { + $results[$provider] = method_exists($this, $func) + ? $this->$func($key) : false; + // If the current provider had no valid data, store nothing: + if (empty($results[$provider])) { + unset($results[$provider]); + } + } catch (\Exception $e) { + // Ignore exceptions: + unset($results[$provider]); + } + } + + return $results; + } + /** * Get an HTTP client * diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php b/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php index 1758ce3f6a7..c243f22d076 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/AuthorNotes.php @@ -48,35 +48,9 @@ class AuthorNotes extends AbstractSyndetics */ public function __invoke($isbn) { - if (!$this->setIsbn($isbn)) { - return array(); - } - - $results = array(); - - // Fetch from provider - if (isset($this->config->Content->authorNotes)) { - $providers = explode(',', $this->config->Content->authorNotes); - foreach ($providers as $provider) { - $parts = explode(':', trim($provider)); - $provider = strtolower($parts[0]); - $func = 'load' . ucwords($provider); - $key = $parts[1]; - try { - $results[$provider] = method_exists($this, $func) - ? $this->$func($key) : false; - // If the current provider had no valid notes, store nothing: - if (empty($results[$provider])) { - unset($results[$provider]); - } - } catch (\Exception $e) { - // Ignore exceptions: - unset($results[$provider]); - } - } - } - - return $results; + $providers = isset($this->config->Content->authorNotes) + ? $this->config->Content->authorNotes : ''; + return $this->getResults($isbn, $providers); } /** diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php b/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php index f0ff97963e0..300b6a89165 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/Excerpt.php @@ -48,35 +48,9 @@ class Excerpt extends AbstractSyndetics */ public function __invoke($isbn) { - if (!$this->setIsbn($isbn)) { - return array(); - } - - $results = array(); - - // Fetch from provider - if (isset($this->config->Content->excerpts)) { - $providers = explode(',', $this->config->Content->excerpts); - foreach ($providers as $provider) { - $parts = explode(':', trim($provider)); - $provider = strtolower($parts[0]); - $func = 'load' . ucwords($provider); - $key = $parts[1]; - try { - $results[$provider] = method_exists($this, $func) ? - $this->$func($key) : false; - // If the current provider had no valid excerpts, store nothing: - if (empty($results[$provider])) { - unset($results[$provider]); - } - } catch (\Exception $e) { - // Ignore exceptions: - unset($results[$provider]); - } - } - } - - return $results; + $providers = isset($this->config->Content->excerpts) + ? $this->config->Content->excerpts : ''; + return $this->getResults($isbn, $providers); } /** diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php b/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php index ac2aa160b54..d4cd48b7905 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/Reviews.php @@ -48,35 +48,9 @@ class Reviews extends AbstractSyndetics */ public function __invoke($isbn) { - if (!$this->setIsbn($isbn)) { - return array(); - } - - $results = array(); - - // Fetch from provider - if (isset($this->config->Content->reviews)) { - $providers = explode(',', $this->config->Content->reviews); - foreach ($providers as $provider) { - $parts = explode(':', trim($provider)); - $provider = strtolower($parts[0]); - $func = 'load' . ucwords($provider); - $key = $parts[1]; - try { - $results[$provider] = method_exists($this, $func) ? - $this->$func($key) : false; - // If the current provider had no valid excerpts, store nothing: - if (empty($results[$provider])) { - unset($results[$provider]); - } - } catch (\Exception $e) { - // Ignore exceptions: - unset($results[$provider]); - } - } - } - - return $results; + $providers = isset($this->config->Content->reviews) + ? $this->config->Content->reviews : ''; + return $this->getResults($isbn, $providers); } /** diff --git a/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php b/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php index 522e6e3ceab..c1f9da03fae 100644 --- a/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php +++ b/module/VuFind/src/VuFind/Theme/Root/Helper/VideoClips.php @@ -48,35 +48,9 @@ class VideoClips extends AbstractSyndetics */ public function __invoke($isbn) { - if (!$this->setIsbn($isbn)) { - return array(); - } - - $results = array(); - - // Fetch from provider - if (isset($this->config->Content->videoClips)) { - $providers = explode(',', $this->config->Content->videoClips); - foreach ($providers as $provider) { - $parts = explode(':', trim($provider)); - $provider = strtolower($parts[0]); - $func = 'load' . ucwords($provider); - $key = $parts[1]; - try { - $results[$provider] = method_exists($this, $func) ? - $this->$func($key) : false; - // If the current provider had no valid clips, store nothing: - if (empty($results[$provider])) { - unset($results[$provider]); - } - } catch (\Exception $e) { - // Ignore exceptions: - unset($results[$provider]); - } - } - } - - return $results; + $providers = isset($this->config->Content->videoClips) + ? $this->config->Content->videoClips : ''; + return $this->getResults($isbn, $providers); } /** -- GitLab