From d6561e9077f180f8bb45cfda80fbcb5c102f2497 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 17 Jan 2018 08:37:32 -0500
Subject: [PATCH] Refactor caching to a trait. - Removes unnecessary base
 behavior from majority of drivers.

---
 .../src/VuFind/ILS/Driver/AbstractBase.php    |  99 ------------
 .../src/VuFind/ILS/Driver/CacheTrait.php      | 141 ++++++++++++++++++
 module/VuFind/src/VuFind/ILS/Driver/DAIA.php  |   1 +
 .../src/VuFind/ILS/Driver/KohaILSDI.php       |   1 +
 .../src/VuFind/ILS/Driver/SierraRest.php      |   1 +
 .../src/VuFind/ILS/Driver/VoyagerRestful.php  |   1 +
 6 files changed, 145 insertions(+), 99 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php

diff --git a/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php b/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php
index f0a08cf791c..460c3dcd6d8 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php
@@ -27,9 +27,6 @@
  */
 namespace VuFind\ILS\Driver;
 
-use VuFind\Cache\KeyGeneratorTrait;
-use Zend\Cache\Storage\StorageInterface;
-
 /**
  * Default ILS driver base class.
  *
@@ -43,22 +40,6 @@ use Zend\Cache\Storage\StorageInterface;
  */
 abstract class AbstractBase implements DriverInterface
 {
-    use KeyGeneratorTrait;
-
-    /**
-     * Cache for storing ILS data temporarily (e.g. patron blocks)
-     *
-     * @var StorageInterface
-     */
-    protected $cache = null;
-
-    /**
-     * Lifetime of cache (in seconds).
-     *
-     * @var int
-     */
-    protected $cacheLifetime = 30;
-
     /**
      * Driver configuration
      *
@@ -66,18 +47,6 @@ abstract class AbstractBase implements DriverInterface
      */
     protected $config = [];
 
-    /**
-     * Set a cache storage object.
-     *
-     * @param StorageInterface $cache Cache storage interface
-     *
-     * @return void
-     */
-    public function setCacheStorage(StorageInterface $cache = null)
-    {
-        $this->cache = $cache;
-    }
-
     /**
      * Set configuration.
      *
@@ -92,72 +61,4 @@ abstract class AbstractBase implements DriverInterface
     {
         $this->config = $config;
     }
-
-    /**
-     * Helper function for fetching cached data.
-     * Data is cached for up to $this->cacheLifetime seconds so that it would be
-     * faster to process e.g. requests where multiple calls to the backend are made.
-     *
-     * @param string $key Cache entry key
-     *
-     * @return mixed|null Cached entry or null if not cached or expired
-     */
-    protected function getCachedData($key)
-    {
-        // No cache object, no cached results!
-        if (null === $this->cache) {
-            return null;
-        }
-
-        $fullKey = $this->getCacheKey($key);
-        $item = $this->cache->getItem($fullKey);
-        if (null !== $item) {
-            // Return value if still valid:
-            if (time() - $item['time'] < $this->cacheLifetime) {
-                return $item['entry'];
-            }
-            // Clear expired item from cache:
-            $this->cache->removeItem($fullKey);
-        }
-        return null;
-    }
-
-    /**
-     * Helper function for storing cached data.
-     * Data is cached for up to $this->cacheLifetime seconds so that it would be
-     * faster to process e.g. requests where multiple calls to the backend are made.
-     *
-     * @param string $key   Cache entry key
-     * @param mixed  $entry Entry to be cached
-     *
-     * @return void
-     */
-    protected function putCachedData($key, $entry)
-    {
-        // Don't write to cache if we don't have a cache!
-        if (null === $this->cache) {
-            return;
-        }
-        $item = [
-            'time' => time(),
-            'entry' => $entry
-        ];
-        $this->cache->setItem($this->getCacheKey($key), $item);
-    }
-
-    /**
-     * Helper function for removing cached data.
-     *
-     * @param string $key Cache entry key
-     *
-     * @return void
-     */
-    protected function removeCachedData($key)
-    {
-        // Don't write to cache if we don't have a cache!
-        if (null === $this->cache) {
-            return;
-        }
-        $this->cache->removeItem($this->getCacheKey($key));
-    }
 }
diff --git a/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php b/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php
new file mode 100644
index 00000000000..8e297550522
--- /dev/null
+++ b/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php
@@ -0,0 +1,141 @@
+<?php
+/**
+ * Trait for ILS drivers using cache.
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2007.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * @category VuFind
+ * @package  ILS_Drivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
+ */
+namespace VuFind\ILS\Driver;
+
+use VuFind\Cache\KeyGeneratorTrait;
+use Zend\Cache\Storage\StorageInterface;
+
+/**
+ * Default ILS driver base class.
+ *
+ * @category VuFind
+ * @package  ILS_Drivers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
+ *
+ * @SuppressWarnings(PHPMD.NumberOfChildren)
+ */
+trait CacheTrait
+{
+    use KeyGeneratorTrait;
+
+    /**
+     * Cache for storing ILS data temporarily (e.g. patron blocks)
+     *
+     * @var StorageInterface
+     */
+    protected $cache = null;
+
+    /**
+     * Lifetime of cache (in seconds).
+     *
+     * @var int
+     */
+    protected $cacheLifetime = 30;
+
+    /**
+     * Set a cache storage object.
+     *
+     * @param StorageInterface $cache Cache storage interface
+     *
+     * @return void
+     */
+    public function setCacheStorage(StorageInterface $cache = null)
+    {
+        $this->cache = $cache;
+    }
+
+    /**
+     * Helper function for fetching cached data.
+     * Data is cached for up to $this->cacheLifetime seconds so that it would be
+     * faster to process e.g. requests where multiple calls to the backend are made.
+     *
+     * @param string $key Cache entry key
+     *
+     * @return mixed|null Cached entry or null if not cached or expired
+     */
+    protected function getCachedData($key)
+    {
+        // No cache object, no cached results!
+        if (null === $this->cache) {
+            return null;
+        }
+
+        $fullKey = $this->getCacheKey($key);
+        $item = $this->cache->getItem($fullKey);
+        if (null !== $item) {
+            // Return value if still valid:
+            if (time() - $item['time'] < $this->cacheLifetime) {
+                return $item['entry'];
+            }
+            // Clear expired item from cache:
+            $this->cache->removeItem($fullKey);
+        }
+        return null;
+    }
+
+    /**
+     * Helper function for storing cached data.
+     * Data is cached for up to $this->cacheLifetime seconds so that it would be
+     * faster to process e.g. requests where multiple calls to the backend are made.
+     *
+     * @param string $key   Cache entry key
+     * @param mixed  $entry Entry to be cached
+     *
+     * @return void
+     */
+    protected function putCachedData($key, $entry)
+    {
+        // Don't write to cache if we don't have a cache!
+        if (null === $this->cache) {
+            return;
+        }
+        $item = [
+            'time' => time(),
+            'entry' => $entry
+        ];
+        $this->cache->setItem($this->getCacheKey($key), $item);
+    }
+
+    /**
+     * Helper function for removing cached data.
+     *
+     * @param string $key Cache entry key
+     *
+     * @return void
+     */
+    protected function removeCachedData($key)
+    {
+        // Don't write to cache if we don't have a cache!
+        if (null === $this->cache) {
+            return;
+        }
+        $this->cache->removeItem($this->getCacheKey($key));
+    }
+}
diff --git a/module/VuFind/src/VuFind/ILS/Driver/DAIA.php b/module/VuFind/src/VuFind/ILS/Driver/DAIA.php
index f932d2306af..06b7d6ee10b 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/DAIA.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/DAIA.php
@@ -51,6 +51,7 @@ use Zend\Log\LoggerAwareInterface as LoggerAwareInterface;
 class DAIA extends AbstractBase implements
     HttpServiceAwareInterface, LoggerAwareInterface
 {
+    use CacheTrait;
     use \VuFindHttp\HttpServiceAwareTrait;
     use \VuFind\Log\LoggerAwareTrait;
 
diff --git a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
index c070a12b1be..c6e44ca76b3 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
@@ -49,6 +49,7 @@ use Zend\Log\LoggerInterface;
 class KohaILSDI extends \VuFind\ILS\Driver\AbstractBase implements
     \VuFindHttp\HttpServiceAwareInterface, \Zend\Log\LoggerAwareInterface
 {
+    use CacheTrait;
     use \VuFindHttp\HttpServiceAwareTrait;
     use \VuFind\Log\LoggerAwareTrait;
 
diff --git a/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php b/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php
index 051de58e90f..b7ff9cfb3f4 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php
@@ -45,6 +45,7 @@ use Zend\Log\LoggerAwareInterface;
 class SierraRest extends AbstractBase implements TranslatorAwareInterface,
     HttpServiceAwareInterface, LoggerAwareInterface
 {
+    use CacheTrait;
     use \VuFind\Log\LoggerAwareTrait {
         logError as error;
     }
diff --git a/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php b/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php
index e8e251619b4..b14a77097e6 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php
@@ -50,6 +50,7 @@ use VuFind\Exception\ILS as ILSException;
  */
 class VoyagerRestful extends Voyager implements \VuFindHttp\HttpServiceAwareInterface
 {
+    use CacheTrait;
     use \VuFindHttp\HttpServiceAwareTrait;
 
     /**
-- 
GitLab