From ea9602b14ffc5658ab8c4fa2a21fbd99428b6f5e Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 21 Dec 2012 10:04:34 -0500
Subject: [PATCH] Set up SRU class to accept injected HTTP client.

---
 module/VuFind/config/module.config.php               |  6 +++++-
 module/VuFind/src/VuFind/Connection/SRU.php          | 12 ++++++------
 module/VuFind/src/VuFind/Connection/WorldCat.php     |  6 ++++--
 module/VuFind/src/VuFind/Harvester/NAF.php           |  6 ++++--
 .../VuFindConsole/Controller/HarvestController.php   |  4 +++-
 5 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index a62331b7bea..cbef5774424 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -464,6 +464,11 @@ $config = array(
 
                 return $translator;
             },
+            'VuFind\WorldCatConnection' => function ($sm) {
+                return new \VuFind\Connection\WorldCat(
+                    $sm->get('VuFind\Http')->createClient()
+                );
+            },
         ),
         'invokables' => array(
             'VuFind\AuthManager' => 'VuFind\Auth\Manager',
@@ -473,7 +478,6 @@ $config = array(
             'VuFind\RecordLoader' => 'VuFind\Record\Loader',
             'VuFind\SearchSpecsReader' => 'VuFind\Config\SearchSpecsReader',
             'VuFind\SessionManager' => 'Zend\Session\SessionManager',
-            'VuFind\WorldCatConnection' => 'VuFind\Connection\WorldCat',
             'VuFind\WorldCatUtils' => 'VuFind\Connection\WorldCatUtils',
         ),
         'initializers' => array(
diff --git a/module/VuFind/src/VuFind/Connection/SRU.php b/module/VuFind/src/VuFind/Connection/SRU.php
index 5dc542832f6..db414b43aa3 100644
--- a/module/VuFind/src/VuFind/Connection/SRU.php
+++ b/module/VuFind/src/VuFind/Connection/SRU.php
@@ -26,8 +26,7 @@
  * @link     http://vufind.org/wiki/system_classes#searching Wiki
  */
 namespace VuFind\Connection;
-use VuFind\Http\Client as HttpClient, VuFind\XSLT\Processor as XSLTProcessor,
-    Zend\Log\LoggerInterface;
+use VuFind\XSLT\Processor as XSLTProcessor, Zend\Log\LoggerInterface;
 
 /**
  * SRU Search Interface
@@ -57,7 +56,7 @@ class SRU implements \Zend\Log\LoggerAwareInterface
     /**
      * The HTTP_Request object used for REST transactions
      *
-     * @var HttpClient
+     * @var \Zend\Http\Client
      */
     protected $client;
 
@@ -80,13 +79,14 @@ class SRU implements \Zend\Log\LoggerAwareInterface
      *
      * Sets up the SOAP Client
      *
-     * @param string $host The URL of the eXist Server
+     * @param string            $host   The URL of the SRU Server
+     * @param \Zend\Http\Client $client An HTTP client object
      */
-    public function __construct($host)
+    public function __construct($host, \Zend\Http\Client $client)
     {
         // Initialize properties needed for HTTP connection:
         $this->host = $host;
-        $this->client = new HttpClient();
+        $this->client = $client;
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Connection/WorldCat.php b/module/VuFind/src/VuFind/Connection/WorldCat.php
index 66274126343..e486229c824 100644
--- a/module/VuFind/src/VuFind/Connection/WorldCat.php
+++ b/module/VuFind/src/VuFind/Connection/WorldCat.php
@@ -56,11 +56,13 @@ class WorldCat extends SRU
 
     /**
      * Constructor
+     *
+     * @param \Zend\Http\Client $client An HTTP client object
      */
-    public function __construct()
+    public function __construct(\Zend\Http\Client $client)
     {
         parent::__construct(
-            'http://www.worldcat.org/webservices/catalog/search/sru'
+            'http://www.worldcat.org/webservices/catalog/search/sru', $client
         );
         $config = ConfigReader::getConfig();
         $this->wskey = isset($config->WorldCat->apiKey)
diff --git a/module/VuFind/src/VuFind/Harvester/NAF.php b/module/VuFind/src/VuFind/Harvester/NAF.php
index 187e3a4306f..2ce27c279f1 100644
--- a/module/VuFind/src/VuFind/Harvester/NAF.php
+++ b/module/VuFind/src/VuFind/Harvester/NAF.php
@@ -52,8 +52,10 @@ class NAF
 
     /**
      * Constructor.
+     *
+     * @param \Zend\Http\Client $client An HTTP client object
      */
-    public function __construct()
+    public function __construct(\Zend\Http\Client $client)
     {
         // Don't time out during harvest!!
         set_time_limit(0);
@@ -78,7 +80,7 @@ class NAF
         $this->loadLastHarvestedDate();
 
         // Set up SRU connection:
-        $this->sru = new SRU('http://alcme.oclc.org/srw/search/lcnaf');
+        $this->sru = new SRU('http://alcme.oclc.org/srw/search/lcnaf', $client);
     }
 
     /**
diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/HarvestController.php b/module/VuFindConsole/src/VuFindConsole/Controller/HarvestController.php
index 67c11bb21c7..36bb87353ad 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/HarvestController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/HarvestController.php
@@ -52,7 +52,9 @@ class HarvestController extends AbstractBase
         // Perform the harvest. Note that first command line parameter
         // may be used to start at a particular date.
         try {
-            $harvest = new NAF();
+            $harvest = new NAF(
+                $this->getServiceLocator()->get('VuFind\Http')->createClient()
+            );
             $argv = $this->consoleOpts->getRemainingArgs();
             if (isset($argv[0])) {
                 $harvest->setStartDate($argv[0]);
-- 
GitLab