From 0ed32c78b87d35c59650b9f59f35a17908f316ab Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 2 Feb 2018 12:56:21 -0500 Subject: [PATCH] Modernize ReCaptcha service. - Use fully qualified class name as service name. - Eliminate static factory. --- module/VuFind/config/module.config.php | 3 +- .../src/VuFind/Controller/Plugin/Factory.php | 5 +- module/VuFind/src/VuFind/Service/Factory.php | 33 ------- .../src/VuFind/Service/ReCaptchaFactory.php | 85 +++++++++++++++++++ .../VuFind/View/Helper/Bootstrap3/Factory.php | 2 +- .../src/VuFind/View/Helper/Root/Factory.php | 2 +- 6 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 module/VuFind/src/VuFind/Service/ReCaptchaFactory.php diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 2dbe8137108..0ec07e4b4fa 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -318,7 +318,6 @@ $config = [ 'VuFind\Log\Logger' => 'VuFind\Log\LoggerFactory', 'VuFind\Mailer\Mailer' => 'VuFind\Mailer\Factory', 'VuFind\Net\IpAddressUtils' => 'Zend\ServiceManager\Factory\InvokableFactory', - 'VuFind\Recaptcha' => 'VuFind\Service\Factory::getRecaptcha', 'VuFind\Recommend\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory', 'VuFind\RecordCache' => 'VuFind\Service\Factory::getRecordCache', 'VuFind\RecordDriver\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory', @@ -340,6 +339,7 @@ $config = [ 'VuFind\SearchRunner' => 'VuFind\Service\Factory::getSearchRunner', 'VuFind\SearchSpecsReader' => 'VuFind\Service\Factory::getSearchSpecsReader', 'VuFind\SearchTabsHelper' => 'VuFind\Service\Factory::getSearchTabsHelper', + 'VuFind\Service\ReCaptcha' => 'VuFind\Service\ReCaptchaFactory', 'VuFind\Session\Settings' => 'Zend\ServiceManager\Factory\InvokableFactory', 'VuFind\SessionManager' => 'VuFind\Session\ManagerFactory', 'VuFind\SessionPluginManager' => 'VuFind\Service\Factory::getSessionPluginManager', @@ -395,6 +395,7 @@ $config = [ 'VuFind\Logger' => 'VuFind\Log\Logger', 'VuFind\Mailer' => 'VuFind\Mailer\Mailer', 'VuFind\ProxyConfig' => 'ProxyManager\Configuration', + 'VuFind\Recaptcha' => 'VuFind\Service\ReCaptcha', 'VuFind\RecommendPluginManager' => 'VuFind\Recommend\PluginManager', 'VuFind\RecordDriverPluginManager' => 'VuFind\RecordDriver\PluginManager', 'VuFind\RecordTabPluginManager' => 'VuFind\RecordTab\PluginManager', diff --git a/module/VuFind/src/VuFind/Controller/Plugin/Factory.php b/module/VuFind/src/VuFind/Controller/Plugin/Factory.php index 16cbac7117c..be6ebe60359 100644 --- a/module/VuFind/src/VuFind/Controller/Plugin/Factory.php +++ b/module/VuFind/src/VuFind/Controller/Plugin/Factory.php @@ -159,10 +159,7 @@ class Factory public static function getRecaptcha(ServiceManager $sm) { $config = $sm->get('VuFind\Config\PluginManager')->get('config'); - return new Recaptcha( - $sm->get('VuFind\Recaptcha'), - $config - ); + return new Recaptcha($sm->get('VuFind\Service\ReCaptcha'), $config); } /** diff --git a/module/VuFind/src/VuFind/Service/Factory.php b/module/VuFind/src/VuFind/Service/Factory.php index 86cbec29e07..2bac4164e58 100644 --- a/module/VuFind/src/VuFind/Service/Factory.php +++ b/module/VuFind/src/VuFind/Service/Factory.php @@ -116,39 +116,6 @@ class Factory return $config; } - /** - * Construct the recaptcha helper - * - * @param ServiceManager $sm Service manager. - * - * @return \VuFind\Record\Loader - */ - public static function getRecaptcha(ServiceManager $sm) - { - $config = $sm->get('VuFind\Config\PluginManager')->get('config'); - $siteKey = isset($config->Captcha->siteKey) - ? $config->Captcha->siteKey - : (isset($config->Captcha->publicKey) - ? $config->Captcha->publicKey - : ''); - $secretKey = isset($config->Captcha->secretKey) - ? $config->Captcha->secretKey - : (isset($config->Captcha->privateKey) - ? $config->Captcha->privateKey - : ''); - $httpClient = $sm->get('VuFindHttp\HttpService')->createClient(); - $translator = $sm->get('VuFind\Translator'); - $options = ['lang' => $translator->getLocale()]; - if (isset($config->Captcha->theme)) { - $options['theme'] = $config->Captcha->theme; - } - $recaptcha = new \VuFind\Service\ReCaptcha( - $siteKey, $secretKey, ['ssl' => true], $options, null, $httpClient - ); - - return $recaptcha; - } - /** * Construct the record cache. * diff --git a/module/VuFind/src/VuFind/Service/ReCaptchaFactory.php b/module/VuFind/src/VuFind/Service/ReCaptchaFactory.php new file mode 100644 index 00000000000..28a902f60a5 --- /dev/null +++ b/module/VuFind/src/VuFind/Service/ReCaptchaFactory.php @@ -0,0 +1,85 @@ +<?php +/** + * ReCaptcha 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 Service + * @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\Service; + +use Interop\Container\ContainerInterface; +use Zend\ServiceManager\Factory\FactoryInterface; + +/** + * ReCaptcha factory. + * + * @category VuFind + * @package Service + * @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 ReCaptchaFactory 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 passed to factory.'); + } + $config = $container->get('VuFind\Config\PluginManager')->get('config'); + $siteKey = isset($config->Captcha->siteKey) + ? $config->Captcha->siteKey + : (isset($config->Captcha->publicKey) + ? $config->Captcha->publicKey + : ''); + $secretKey = isset($config->Captcha->secretKey) + ? $config->Captcha->secretKey + : (isset($config->Captcha->privateKey) + ? $config->Captcha->privateKey + : ''); + $httpClient = $container->get('VuFindHttp\HttpService')->createClient(); + $translator = $container->get('VuFind\Translator'); + $rcOptions = ['lang' => $translator->getLocale()]; + if (isset($config->Captcha->theme)) { + $rcOptions['theme'] = $config->Captcha->theme; + } + return new $requestedName( + $siteKey, $secretKey, ['ssl' => true], $rcOptions, null, $httpClient + ); + } +} diff --git a/module/VuFind/src/VuFind/View/Helper/Bootstrap3/Factory.php b/module/VuFind/src/VuFind/View/Helper/Bootstrap3/Factory.php index 0859d3eb3a8..31946da81be 100644 --- a/module/VuFind/src/VuFind/View/Helper/Bootstrap3/Factory.php +++ b/module/VuFind/src/VuFind/View/Helper/Bootstrap3/Factory.php @@ -92,7 +92,7 @@ class Factory public static function getRecaptcha(ServiceManager $sm) { return new Recaptcha( - $sm->get('VuFind\Recaptcha'), + $sm->get('VuFind\Service\ReCaptcha'), $sm->get('VuFind\Config\PluginManager')->get('config') ); } diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php index 10f1c3de446..68ac411633d 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php @@ -402,7 +402,7 @@ class Factory public static function getRecaptcha(ServiceManager $sm) { return new Recaptcha( - $sm->get('VuFind\Recaptcha'), + $sm->get('VuFind\Service\ReCaptcha'), $sm->get('VuFind\Config\PluginManager')->get('config') ); } -- GitLab