From cf0d7e87bef1721f77ed93da5b0bb8c9549aa740 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 5 Feb 2018 16:54:38 -0500
Subject: [PATCH] Modernize WorldCatUtils service. - Use fully qualified class
 name as service name. - Eliminate static factory.

---
 module/VuFind/config/module.config.php        |  3 +-
 .../Connection/WorldCatUtilsFactory.php       | 72 +++++++++++++++++++
 .../VuFind/src/VuFind/Recommend/Factory.php   |  4 +-
 module/VuFind/src/VuFind/Service/Factory.php  | 18 -----
 4 files changed, 75 insertions(+), 22 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Connection/WorldCatUtilsFactory.php

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 246aa1d8d9b..86dd1dcf826 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -345,7 +345,7 @@ $config = [
             'VuFind\SMS\SMSInterface' => 'VuFind\SMS\Factory',
             'VuFind\Solr\Writer' => 'VuFind\Solr\WriterFactory',
             'VuFind\Tags' => 'VuFind\TagsFactory',
-            'VuFind\WorldCatUtils' => 'VuFind\Service\Factory::getWorldCatUtils',
+            'VuFind\Connection\WorldCatUtils' => 'VuFind\Connection\WorldCatUtilsFactory',
             'VuFindHttp\HttpService' => 'VuFind\Service\Factory::getHttp',
             'VuFindSearch\Service' => 'VuFind\Service\Factory::getSearchService',
             'Zend\Db\Adapter\Adapter' => 'VuFind\Service\Factory::getDbAdapter',
@@ -413,6 +413,7 @@ $config = [
             'VuFind\SessionPluginManager' => 'VuFind\Session\PluginManager',
             'VuFind\SMS' => 'VuFind\SMS\SMSInterface',
             'VuFind\Translator' => 'Zend\Mvc\I18n\Translator',
+            'VuFind\WorldCatUtils' => 'VuFind\Connection\WorldCatUtils',
             'VuFind\YamlReader' => 'VuFind\Config\YamlReader',
         ],
     ],
diff --git a/module/VuFind/src/VuFind/Connection/WorldCatUtilsFactory.php b/module/VuFind/src/VuFind/Connection/WorldCatUtilsFactory.php
new file mode 100644
index 00000000000..d8884a7b034
--- /dev/null
+++ b/module/VuFind/src/VuFind/Connection/WorldCatUtilsFactory.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * WorldCat utils factory.
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2018.
+ *
+ * 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  WorldCat
+ * @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 Wiki
+ */
+namespace VuFind\Connection;
+
+use Interop\Container\ContainerInterface;
+use Zend\ServiceManager\Factory\FactoryInterface;
+
+/**
+ * WorldCat utils factory.
+ *
+ * @category VuFind
+ * @package  WorldCat
+ * @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 Wiki
+ */
+class WorldCatUtilsFactory implements FactoryInterface
+{
+    /**
+     * Create an object
+     *
+     * @param ContainerInterface $container     Service manager
+     * @param string             $requestedName Service being created
+     * @param null|array         $options       Extra options (optional)
+     *
+     * @return object
+     *
+     * @throws ServiceNotFoundException if unable to resolve the service.
+     * @throws ServiceNotCreatedException if an exception is raised when
+     * creating a service.
+     * @throws ContainerException if any other error occurs
+     */
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options sent to factory.');
+        }
+        $config = $container->get('VuFind\Config\PluginManager')->get('config');
+        $client = $container->get('VuFindHttp\HttpService')->createClient();
+        $ip = $container->get('Request')->getServer()->get('SERVER_ADDR');
+        return new $requestedName(
+            isset($config->WorldCat) ? $config->WorldCat : null,
+            $client, true, $ip
+        );
+    }
+}
diff --git a/module/VuFind/src/VuFind/Recommend/Factory.php b/module/VuFind/src/VuFind/Recommend/Factory.php
index 56f03bc3570..15f60ca437b 100644
--- a/module/VuFind/src/VuFind/Recommend/Factory.php
+++ b/module/VuFind/src/VuFind/Recommend/Factory.php
@@ -342,8 +342,6 @@ class Factory
      */
     public static function getWorldCatIdentities(ServiceManager $sm)
     {
-        return new WorldCatIdentities(
-            $sm->get('VuFind\WorldCatUtils')
-        );
+        return new WorldCatIdentities($sm->get('VuFind\Connection\WorldCatUtils'));
     }
 }
diff --git a/module/VuFind/src/VuFind/Service/Factory.php b/module/VuFind/src/VuFind/Service/Factory.php
index bf3766f7adb..d141178a6b3 100644
--- a/module/VuFind/src/VuFind/Service/Factory.php
+++ b/module/VuFind/src/VuFind/Service/Factory.php
@@ -165,22 +165,4 @@ class Factory
 
         return $translator;
     }
-
-    /**
-     * Construct the WorldCat helper.
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return \VuFind\Connection\WorldCatUtils
-     */
-    public static function getWorldCatUtils(ServiceManager $sm)
-    {
-        $config = $sm->get('VuFind\Config\PluginManager')->get('config');
-        $client = $sm->get('VuFindHttp\HttpService')->createClient();
-        $ip = $sm->get('Request')->getServer()->get('SERVER_ADDR');
-        return new \VuFind\Connection\WorldCatUtils(
-            isset($config->WorldCat) ? $config->WorldCat : null,
-            $client, true, $ip
-        );
-    }
 }
-- 
GitLab