From ac4239ec05e647c4c9b8cd59b64475235a21a23c Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 27 Feb 2013 11:39:09 -0500
Subject: [PATCH] Created services for hold/title-hold logic. Simplified some
 method signatures thanks to the new dependency injection approach. Decoupled
 ILS\Logic classes from Config\Reader.

---
 module/VuFind/config/module.config.php        | 22 ++++-
 .../src/VuFind/Controller/AjaxController.php  |  7 +-
 module/VuFind/src/VuFind/ILS/Logic/Holds.php  | 11 +--
 .../src/VuFind/ILS/Logic/TitleHolds.php       | 11 +--
 .../src/VuFind/RecordDriver/SolrDefault.php   |  4 +-
 .../src/VuFind/RecordDriver/SolrMarc.php      | 82 +++++++++++++------
 .../src/VuFind/RecordDriver/WorldCat.php      |  4 +-
 .../templates/RecordTab/holdingsils.phtml     |  4 +-
 .../templates/RecordTab/holdingsils.phtml     |  4 +-
 themes/root/templates/Email/record-sms.phtml  |  2 +-
 10 files changed, 100 insertions(+), 51 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 2e08e6eaa1e..d4dc2d5cf18 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -167,11 +167,25 @@ $config = array(
                         $sm->get('VuFind\ILSDriverPluginManager')
                     );
             },
+            'VuFind\ILSHoldLogic' => function ($sm) {
+                return new \VuFind\ILS\Logic\Holds(
+                    $sm->get('VuFind\AuthManager'),
+                    $sm->get('VuFind\ILSConnection'),
+                    $sm->get('VuFind\Config')->get('config')
+                );
+            },
             'VuFind\ILSHoldSettings' => function ($sm) {
                 return new \VuFind\ILS\HoldSettings(
                     $sm->get('VuFind\Config')->get('config')->Catalog
                 );
             },
+            'VuFind\ILSTitleHoldLogic' => function ($sm) {
+                return new \VuFind\ILS\Logic\TitleHolds(
+                    $sm->get('VuFind\AuthManager'),
+                    $sm->get('VuFind\ILSConnection'),
+                    $sm->get('VuFind\Config')->get('config')
+                );
+            },
             'VuFind\Logger' => function ($sm) {
                 $logger = new \VuFind\Log\Logger();
                 $logger->setServiceLocator($sm);
@@ -489,11 +503,17 @@ $config = array(
                         );
                     },
                     'solrmarc' => function ($sm) {
-                        return new \VuFind\RecordDriver\SolrMarc(
+                        $driver = new \VuFind\RecordDriver\SolrMarc(
                             $sm->getServiceLocator()->get('VuFind\Config')->get('config'),
                             null,
                             $sm->getServiceLocator()->get('VuFind\Config')->get('searches')
                         );
+                        $driver->attachILS(
+                            $sm->getServiceLocator()->get('VuFind\ILSConnection'),
+                            $sm->getServiceLocator()->get('VuFind\ILSHoldLogic'),
+                            $sm->getServiceLocator()->get('VuFind\ILSTitleHoldLogic')
+                        );
+                        return $driver;
                     },
                     'solrreserves' => function ($sm) {
                         return new \VuFind\RecordDriver\SolrReserves(
diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php
index 6f64c5d8508..5d19f4e1248 100644
--- a/module/VuFind/src/VuFind/Controller/AjaxController.php
+++ b/module/VuFind/src/VuFind/Controller/AjaxController.php
@@ -149,15 +149,14 @@ class AjaxController extends AbstractBase
      *
      * @param array                  $record  Information on items linked to a single
      * bib record
-     * @param \VuFind\ILS\Connection $catalog ILS connection
      *
      * @return array        Filtered version of $record
      */
-    protected function filterSuppressedLocations($record, $catalog)
+    protected function filterSuppressedLocations($record)
     {
         static $hideHoldings = false;
         if ($hideHoldings === false) {
-            $logic = new \VuFind\ILS\Logic\Holds($this->getAuthManager(), $catalog);
+            $logic = $this->getServiceLocator()->get('VuFind\ILSHoldLogic');
             $hideHoldings = $logic->getSuppressedLocations();
         }
 
@@ -222,7 +221,7 @@ class AjaxController extends AbstractBase
         $statuses = array();
         foreach ($results as $recordNumber=>$record) {
             // Filter out suppressed locations:
-            $record = $this->filterSuppressedLocations($record, $catalog);
+            $record = $this->filterSuppressedLocations($record);
 
             // Skip empty records:
             if (count($record)) {
diff --git a/module/VuFind/src/VuFind/ILS/Logic/Holds.php b/module/VuFind/src/VuFind/ILS/Logic/Holds.php
index ed34576f50b..e728352a10c 100644
--- a/module/VuFind/src/VuFind/ILS/Logic/Holds.php
+++ b/module/VuFind/src/VuFind/ILS/Logic/Holds.php
@@ -27,8 +27,7 @@
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
 namespace VuFind\ILS\Logic;
-use VuFind\Config\Reader as ConfigReader, VuFind\Crypt\HMAC,
-    VuFind\ILS\Connection as ILSConnection;
+use VuFind\Crypt\HMAC, VuFind\ILS\Connection as ILSConnection;
 
 /**
  * Hold Logic Class
@@ -75,11 +74,13 @@ class Holds
      *
      * @param \VuFind\Auth\Manager $account Auth manager object
      * @param ILSConnection        $ils     A catalog connection
+     * @param \Zend\Config\Config  $config  VuFind configuration
      */
-    public function __construct(\VuFind\Auth\Manager $account, ILSConnection $ils)
-    {
+    public function __construct(\VuFind\Auth\Manager $account, ILSConnection $ils,
+        \Zend\Config\Config $config
+    ) {
         $this->account = $account;
-        $this->config = ConfigReader::getConfig();
+        $this->config = $config;
 
         if (isset($this->config->Record->hide_holdings)) {
             foreach ($this->config->Record->hide_holdings as $current) {
diff --git a/module/VuFind/src/VuFind/ILS/Logic/TitleHolds.php b/module/VuFind/src/VuFind/ILS/Logic/TitleHolds.php
index 8d6e517fc83..2431c8f06b0 100644
--- a/module/VuFind/src/VuFind/ILS/Logic/TitleHolds.php
+++ b/module/VuFind/src/VuFind/ILS/Logic/TitleHolds.php
@@ -27,8 +27,7 @@
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
 namespace VuFind\ILS\Logic;
-use VuFind\Config\Reader as ConfigReader, VuFind\Crypt\HMAC,
-    VuFind\ILS\Connection as ILSConnection;
+use VuFind\Crypt\HMAC, VuFind\ILS\Connection as ILSConnection;
 
 /**
  * Title Hold Logic Class
@@ -75,11 +74,13 @@ class TitleHolds
      *
      * @param \VuFind\Auth\Manager $account Auth manager object
      * @param ILSConnection        $ils     A catalog connection
+     * @param \Zend\Config\Config  $config  VuFind configuration
      */
-    public function __construct(\VuFind\Auth\Manager $account, ILSConnection $ils)
-    {
+    public function __construct(\VuFind\Auth\Manager $account, ILSConnection $ils,
+        \Zend\Config\Config $config
+    ) {
         $this->account = $account;
-        $this->config = ConfigReader::getConfig();
+        $this->config = $config;
 
         if (isset($this->config->Record->hide_holdings)) {
             foreach ($this->config->Record->hide_holdings as $current) {
diff --git a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
index 57b2cbe0841..e7807ffbb8f 100644
--- a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
+++ b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
@@ -875,11 +875,9 @@ class SolrDefault extends AbstractBase
      * Get an array of information about record holdings, obtained in real-time
      * from the ILS.
      *
-     * @param \VuFind\Auth\Manager $account Auth manager object
-     *
      * @return array
      */
-    public function getRealTimeHoldings(\VuFind\Auth\Manager $account)
+    public function getRealTimeHoldings()
     {
         // Not supported by the Solr index -- implement in child classes.
         return array();
diff --git a/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php b/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php
index b1101041bda..22669543d8b 100644
--- a/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php
+++ b/module/VuFind/src/VuFind/RecordDriver/SolrMarc.php
@@ -26,10 +26,7 @@
  * @link     http://vufind.org/wiki/vufind2:record_drivers Wiki
  */
 namespace VuFind\RecordDriver;
-use VuFind\Exception\ILS as ILSException,
-    VuFind\ILS\Logic\Holds as HoldLogic,
-    VuFind\ILS\Logic\TitleHolds as TitleHoldLogic,
-    VuFind\XSLT\Processor as XSLTProcessor;
+use VuFind\Exception\ILS as ILSException, VuFind\XSLT\Processor as XSLTProcessor;
 
 /**
  * Model for MARC records in Solr.
@@ -49,6 +46,27 @@ class SolrMarc extends SolrDefault
      */
     protected $marcRecord;
 
+    /**
+     * ILS connection
+     *
+     * @var \VuFind\ILS\Connection
+     */
+    protected $ils = null;
+
+    /**
+     * Hold logic
+     *
+     * @var \VuFind\ILS\Logic\Holds
+     */
+    protected $holdLogic;
+
+    /**
+     * Title hold logic
+     *
+     * @var \VuFind\ILS\Logic\TitleHolds
+     */
+    protected $titleHoldLogic;
+
     /**
      * Set raw data to initialize the object.
      *
@@ -914,28 +932,42 @@ class SolrMarc extends SolrDefault
     }
 
     /**
-     * Get the ILS connection.
+     * Attach an ILS connection and related logic to the driver
      *
-     * @return \VuFind\ILS\Connection
+     * @param \VuFind\ILS\Connection       $ils            ILS connection
+     * @param \VuFind\ILS\Logic\Holds      $holdLogic      Hold logic handler
+     * @param \VuFind\ILS\Logic\TitleHolds $titleHoldLogic Title hold logic handler
      */
-    protected function getILS()
+    public function attachILS(\VuFind\ILS\Connection $ils,
+        \VuFind\ILS\Logic\Holds $holdLogic,
+        \VuFind\ILS\Logic\TitleHolds $titleHoldLogic
+    ) {
+        $this->ils = $ils;
+        $this->holdLogic = $holdLogic;
+        $this->titleHoldLogic = $titleHoldLogic;
+    }
+
+    /**
+     * Do we have an attached ILS connection?
+     *
+     * @return bool
+     */
+    protected function hasILS()
     {
-        return $this->getServiceLocator()->getServiceLocator()
-            ->get('VuFind\ILSConnection');
+        return null !== $this->ils;
     }
 
     /**
      * Get an array of information about record holdings, obtained in real-time
      * from the ILS.
      *
-     * @param \VuFind\Auth\Manager $account Auth manager object
-     *
      * @return array
      */
-    public function getRealTimeHoldings(\VuFind\Auth\Manager $account)
+    public function getRealTimeHoldings()
     {
-        $holdLogic = new HoldLogic($account, $this->getILS());
-        return $holdLogic->getHoldings($this->getUniqueID());
+        return $this->hasILS()
+            ? $this->holdLogic->getHoldings($this->getUniqueID())
+            : array();
     }
 
     /**
@@ -947,8 +979,11 @@ class SolrMarc extends SolrDefault
     public function getRealTimeHistory()
     {
         // Get Acquisitions Data
+        if (!$this->hasILS()) {
+            return array();
+        }
         try {
-            return $this->getILS()->getPurchaseHistory($this->getUniqueID());
+            return $this->ils->getPurchaseHistory($this->getUniqueID());
         } catch (ILSException $e) {
             return array();
         }
@@ -957,19 +992,16 @@ class SolrMarc extends SolrDefault
     /**
      * Get a link for placing a title level hold.
      *
-     * @param \VuFind\Auth\Manager $account Auth manager object
-     *
      * @return mixed A url if a hold is possible, boolean false if not
      */
-    public function getRealTimeTitleHold(\VuFind\Auth\Manager $account)
+    public function getRealTimeTitleHold()
     {
-        $biblioLevel = $this->getBibliographicLevel();
-        if ("monograph" == strtolower($biblioLevel)
-            || stristr("part", $biblioLevel)
-        ) {
-            if ($this->getILS()->getTitleHoldsMode() != "disabled") {
-                $holdLogic = new TitleHoldLogic($account, $this->getILS());
-                return $holdLogic->getHold($this->getUniqueID());
+        if ($this->hasILS()) {
+            $biblioLevel = strtolower($this->getBibliographicLevel());
+            if ("monograph" == $biblioLevel || strstr("part", $biblioLevel)) {
+                if ($this->ils->getTitleHoldsMode() != "disabled") {
+                    return $this->titleHoldLogic->getHold($this->getUniqueID());
+                }
             }
         }
 
diff --git a/module/VuFind/src/VuFind/RecordDriver/WorldCat.php b/module/VuFind/src/VuFind/RecordDriver/WorldCat.php
index c172fff649d..0510ec6a2ad 100644
--- a/module/VuFind/src/VuFind/RecordDriver/WorldCat.php
+++ b/module/VuFind/src/VuFind/RecordDriver/WorldCat.php
@@ -81,11 +81,9 @@ class WorldCat extends SolrMarc
      * Get an array of information about record holdings, obtained in real-time
      * from the ILS.
      *
-     * @param \VuFind\Auth\Manager $account Auth manager object
-     *
      * @return array
      */
-    public function getRealTimeHoldings(\VuFind\Auth\Manager $account)
+    public function getRealTimeHoldings()
     {
         // Not supported here:
         return array();
diff --git a/themes/blueprint/templates/RecordTab/holdingsils.phtml b/themes/blueprint/templates/RecordTab/holdingsils.phtml
index a4db7a3b2d0..79e862bd3f5 100644
--- a/themes/blueprint/templates/RecordTab/holdingsils.phtml
+++ b/themes/blueprint/templates/RecordTab/holdingsils.phtml
@@ -2,7 +2,7 @@
     // Set up convenience variables:
     $account = $this->auth()->getManager();
     $user = $account->isLoggedIn();
-    $holdings = $this->driver->getRealTimeHoldings($account);
+    $holdings = $this->driver->getRealTimeHoldings();
     $openUrl = $this->driver->openURLActive('holdings') ? $this->driver->getOpenURL() : false;
     $offlineMode = $this->ils()->getOfflineMode();
     // Account for replace_other_urls setting
@@ -33,7 +33,7 @@
     <? endif; ?>
   <? endif; ?>
 <? endif; ?>
-<? $holdingTitleHold = $this->driver->tryMethod('getRealTimeTitleHold', array($account)); if (!empty($holdingTitleHold)): ?>
+<? $holdingTitleHold = $this->driver->tryMethod('getRealTimeTitleHold'); if (!empty($holdingTitleHold)): ?>
     <a class="holdPlace" href="<?=$this->recordLink()->getHoldUrl($holdingTitleHold)?>"><?=$this->transEsc('title_hold_place')?></a>
 <? endif; ?>
 <? if (!empty($urls) || $openUrl): ?>
diff --git a/themes/jquerymobile/templates/RecordTab/holdingsils.phtml b/themes/jquerymobile/templates/RecordTab/holdingsils.phtml
index e49ed4462aa..66f3848768a 100644
--- a/themes/jquerymobile/templates/RecordTab/holdingsils.phtml
+++ b/themes/jquerymobile/templates/RecordTab/holdingsils.phtml
@@ -2,7 +2,7 @@
     // Set up convenience variables:
     $account = $this->auth()->getManager();
     $user = $account->isLoggedIn();
-    $holdings = $this->driver->getRealTimeHoldings($account);
+    $holdings = $this->driver->getRealTimeHoldings();
     $offlineMode = $this->ils()->getOfflineMode();
 
     // Set page title.
@@ -30,7 +30,7 @@
     <? endif; ?>
   <? endif; ?>
 <? endif; ?>
-<? $holdingTitleHold = $this->driver->tryMethod('getRealTimeTitleHold', array($account)); if (!empty($holdingTitleHold)): ?>
+<? $holdingTitleHold = $this->driver->tryMethod('getRealTimeTitleHold'); if (!empty($holdingTitleHold)): ?>
     <a rel="external" class="holdPlace" href="<?=$this->recordLink()->getHoldUrl($holdingTitleHold, false)?>"><?=$this->transEsc('title_hold_place')?></a>
 <? endif; ?>
 <? foreach ($holdings as $location => $holding): ?>
diff --git a/themes/root/templates/Email/record-sms.phtml b/themes/root/templates/Email/record-sms.phtml
index 5bf63173fa1..411c4ce214d 100644
--- a/themes/root/templates/Email/record-sms.phtml
+++ b/themes/root/templates/Email/record-sms.phtml
@@ -7,7 +7,7 @@
     // since text messages can be short, and we want the most important stuff
     // at the top!
     if ($this->driver->supportsAjaxStatus()) {
-        $holdings = $this->driver->getRealTimeHoldings($this->auth()->getManager());
+        $holdings = $this->driver->getRealTimeHoldings();
 
         // Figure out which call number/location to display.  We'll try to find
         // a location with an available item that has a call number.  Failing that,
-- 
GitLab