From dfe2bf838673871c48eb31e7680f9f8ce03e8eea Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 28 Jan 2019 15:24:17 -0500
Subject: [PATCH] Eliminate static permission provider factories.

---
 .../Role/PermissionProvider/Factory.php       | 126 ------------------
 .../InjectAuthorizationServiceFactory.php     |  69 ++++++++++
 .../InjectRequestFactory.php                  |  67 ++++++++++
 .../PermissionProvider/IpRangeFactory.php     |  70 ++++++++++
 .../Role/PermissionProvider/PluginManager.php |  37 +++--
 .../PermissionProvider/ShibbolethFactory.php  |  68 ++++++++++
 6 files changed, 289 insertions(+), 148 deletions(-)
 delete mode 100644 module/VuFind/src/VuFind/Role/PermissionProvider/Factory.php
 create mode 100644 module/VuFind/src/VuFind/Role/PermissionProvider/InjectAuthorizationServiceFactory.php
 create mode 100644 module/VuFind/src/VuFind/Role/PermissionProvider/InjectRequestFactory.php
 create mode 100644 module/VuFind/src/VuFind/Role/PermissionProvider/IpRangeFactory.php
 create mode 100644 module/VuFind/src/VuFind/Role/PermissionProvider/ShibbolethFactory.php

diff --git a/module/VuFind/src/VuFind/Role/PermissionProvider/Factory.php b/module/VuFind/src/VuFind/Role/PermissionProvider/Factory.php
deleted file mode 100644
index efcfbadf7bd..00000000000
--- a/module/VuFind/src/VuFind/Role/PermissionProvider/Factory.php
+++ /dev/null
@@ -1,126 +0,0 @@
-<?php
-/**
- * Permission Provider Factory Class
- *
- * PHP version 7
- *
- * Copyright (C) Villanova University 2014.
- *
- * 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  Authorization
- * @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:hierarchy_components Wiki
- */
-namespace VuFind\Role\PermissionProvider;
-
-use Zend\ServiceManager\ServiceManager;
-
-/**
- * Permission Provider Factory Class
- *
- * @category VuFind
- * @package  Authorization
- * @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:hierarchy_components Wiki
- *
- * @codeCoverageIgnore
- */
-class Factory
-{
-    /**
-     * Factory for IpRange
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return IpRange
-     */
-    public static function getIpRange(ServiceManager $sm)
-    {
-        return new IpRange(
-            $sm->get('Request'),
-            $sm->get('VuFind\Net\IpAddressUtils')
-        );
-    }
-
-    /**
-     * Factory for IpRegEx
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return IpRegEx
-     */
-    public static function getIpRegEx(ServiceManager $sm)
-    {
-        return new IpRegEx($sm->get('Request'));
-    }
-
-    /**
-     * Factory for ServerParam
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return ServerParam
-     */
-    public static function getServerParam(ServiceManager $sm)
-    {
-        return new ServerParam($sm->get('Request'));
-    }
-
-    /**
-     * Factory for Shibboleth
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Shibboleth
-     */
-    public static function getShibboleth(ServiceManager $sm)
-    {
-        return new Shibboleth(
-            $sm->get('Request'),
-            $sm->get('VuFind\Config\PluginManager')->get('config')
-        );
-    }
-
-    /**
-     * Factory for Username
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return Username
-     */
-    public static function getUsername(ServiceManager $sm)
-    {
-        return new Username(
-            $sm->get('ZfcRbac\Service\AuthorizationService')
-        );
-    }
-
-    /**
-     * Factory for User
-     *
-     * @param ServiceManager $sm Service manager.
-     *
-     * @return User
-     */
-    public static function getUser(ServiceManager $sm)
-    {
-        return new User(
-            $sm->get('ZfcRbac\Service\AuthorizationService')
-        );
-    }
-}
diff --git a/module/VuFind/src/VuFind/Role/PermissionProvider/InjectAuthorizationServiceFactory.php b/module/VuFind/src/VuFind/Role/PermissionProvider/InjectAuthorizationServiceFactory.php
new file mode 100644
index 00000000000..ddbc8beead6
--- /dev/null
+++ b/module/VuFind/src/VuFind/Role/PermissionProvider/InjectAuthorizationServiceFactory.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Factory for instantiating permission providers with authorization service.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2019.
+ *
+ * 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  Authorization
+ * @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\Role\PermissionProvider;
+
+use Interop\Container\ContainerInterface;
+use ZfcRbac\Service\AuthorizationService;
+
+/**
+ * Factory for instantiating permission providers with authorization service.
+ *
+ * @category VuFind
+ * @package  Authorization
+ * @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 InjectAuthorizationServiceFactory
+    implements \Zend\ServiceManager\Factory\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
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+     */
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options passed to factory.');
+        }
+        return new $requestedName($container->get(AuthorizationService::class));
+    }
+}
diff --git a/module/VuFind/src/VuFind/Role/PermissionProvider/InjectRequestFactory.php b/module/VuFind/src/VuFind/Role/PermissionProvider/InjectRequestFactory.php
new file mode 100644
index 00000000000..e290b3ceb68
--- /dev/null
+++ b/module/VuFind/src/VuFind/Role/PermissionProvider/InjectRequestFactory.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Factory for instantiating permission providers with request object.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2019.
+ *
+ * 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  Authorization
+ * @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\Role\PermissionProvider;
+
+use Interop\Container\ContainerInterface;
+
+/**
+ * Factory for instantiating permission providers with request object.
+ *
+ * @category VuFind
+ * @package  Authorization
+ * @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 InjectRequestFactory implements \Zend\ServiceManager\Factory\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
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+     */
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options passed to factory.');
+        }
+        return new $requestedName($container->get('Request'));
+    }
+}
diff --git a/module/VuFind/src/VuFind/Role/PermissionProvider/IpRangeFactory.php b/module/VuFind/src/VuFind/Role/PermissionProvider/IpRangeFactory.php
new file mode 100644
index 00000000000..7e681375bf8
--- /dev/null
+++ b/module/VuFind/src/VuFind/Role/PermissionProvider/IpRangeFactory.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Factory for instantiating IpRange permission provider.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2019.
+ *
+ * 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  Authorization
+ * @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\Role\PermissionProvider;
+
+use Interop\Container\ContainerInterface;
+
+/**
+ * Factory for instantiating IpRange permission provider.
+ *
+ * @category VuFind
+ * @package  Authorization
+ * @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 IpRangeFactory implements \Zend\ServiceManager\Factory\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
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+     */
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options passed to factory.');
+        }
+        return new $requestedName(
+            $container->get('Request'),
+            $container->get(\VuFind\Net\IpAddressUtils::class)
+        );
+    }
+}
diff --git a/module/VuFind/src/VuFind/Role/PermissionProvider/PluginManager.php b/module/VuFind/src/VuFind/Role/PermissionProvider/PluginManager.php
index be82fd86c07..fa0ff0cfd69 100644
--- a/module/VuFind/src/VuFind/Role/PermissionProvider/PluginManager.php
+++ b/module/VuFind/src/VuFind/Role/PermissionProvider/PluginManager.php
@@ -44,13 +44,13 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
      * @var array
      */
     protected $aliases = [
-        'ipRange' => 'VuFind\Role\PermissionProvider\IpRange',
-        'ipRegEx' => 'VuFind\Role\PermissionProvider\IpRegEx',
-        'role' => 'VuFind\Role\PermissionProvider\Role',
-        'serverParam' => 'VuFind\Role\PermissionProvider\ServerParam',
-        'shibboleth' => 'VuFind\Role\PermissionProvider\Shibboleth',
-        'user' => 'VuFind\Role\PermissionProvider\User',
-        'username' => 'VuFind\Role\PermissionProvider\Username',
+        'ipRange' => IpRange::class,
+        'ipRegEx' => IpRegEx::class,
+        'role' => Role::class,
+        'serverParam' => ServerParam::class,
+        'shibboleth' => Shibboleth::class,
+        'user' => User::class,
+        'username' => Username::class,
     ];
 
     /**
@@ -59,20 +59,13 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
      * @var array
      */
     protected $factories = [
-        'VuFind\Role\PermissionProvider\IpRange' =>
-            'VuFind\Role\PermissionProvider\Factory::getIpRange',
-        'VuFind\Role\PermissionProvider\IpRegEx' =>
-            'VuFind\Role\PermissionProvider\Factory::getIpRegEx',
-        'VuFind\Role\PermissionProvider\Role' =>
-            'Zend\ServiceManager\Factory\InvokableFactory',
-        'VuFind\Role\PermissionProvider\ServerParam' =>
-            'VuFind\Role\PermissionProvider\Factory::getServerParam',
-        'VuFind\Role\PermissionProvider\Shibboleth' =>
-            'VuFind\Role\PermissionProvider\Factory::getShibboleth',
-        'VuFind\Role\PermissionProvider\User' =>
-            'VuFind\Role\PermissionProvider\Factory::getUser',
-        'VuFind\Role\PermissionProvider\Username' =>
-            'VuFind\Role\PermissionProvider\Factory::getUsername',
+        IpRange::class => IpRangeFactory::class,
+        IpRegEx::class => InjectRequestFactory::class,
+        Role::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
+        ServerParam::class => InjectRequestFactory::class,
+        Shibboleth::class => ShibbolethFactory::class,
+        User::class => InjectAuthorizationServiceFactory::class,
+        Username::class => InjectAuthorizationServiceFactory::class,
     ];
 
     /**
@@ -83,6 +76,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
      */
     protected function getExpectedInterface()
     {
-        return 'VuFind\Role\PermissionProvider\PermissionProviderInterface';
+        return PermissionProviderInterface::class;
     }
 }
diff --git a/module/VuFind/src/VuFind/Role/PermissionProvider/ShibbolethFactory.php b/module/VuFind/src/VuFind/Role/PermissionProvider/ShibbolethFactory.php
new file mode 100644
index 00000000000..9c99caf7156
--- /dev/null
+++ b/module/VuFind/src/VuFind/Role/PermissionProvider/ShibbolethFactory.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Factory for instantiating Shibboleth permission provider.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2019.
+ *
+ * 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  Authorization
+ * @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\Role\PermissionProvider;
+
+use Interop\Container\ContainerInterface;
+
+/**
+ * Factory for instantiating Shibboleth permission provider.
+ *
+ * @category VuFind
+ * @package  Authorization
+ * @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 ShibbolethFactory implements \Zend\ServiceManager\Factory\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
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+     */
+    public function __invoke(ContainerInterface $container, $requestedName,
+        array $options = null
+    ) {
+        if (!empty($options)) {
+            throw new \Exception('Unexpected options passed to factory.');
+        }
+        $cfg = $container->get(\VuFind\Config\PluginManager::class)->get('config');
+        return new $requestedName($container->get('Request'), $cfg);
+    }
+}
-- 
GitLab