From b79887d6e5ebe2e9aab7627a4cd8d5a16137427f Mon Sep 17 00:00:00 2001 From: rarog Date: Thu, 16 Jul 2020 17:02:35 +0200 Subject: [PATCH] Cleaning up factories - Getting rid of old and unused methods used by laminas-servicemanager < 3 - Adjusting method signature of __invoke to be in sync with interface. --- .../Adapter/AdapterChainServiceFactory.php | 25 +++++------- .../Authentication/Adapter/DbFactory.php | 20 +++------- .../Authentication/Storage/DbFactory.php | 20 +++------- src/LmcUser/Factory/AuthenticationService.php | 22 ++++------- .../Plugin/LmcUserAuthentication.php | 24 ++++-------- .../Controller/RedirectCallbackFactory.php | 24 ++++-------- .../Controller/UserControllerFactory.php | 38 +++++++------------ src/LmcUser/Factory/Mapper/User.php | 22 ++++------- src/LmcUser/Factory/Options/ModuleOptions.php | 20 +++------- src/LmcUser/Factory/Service/UserFactory.php | 20 +++------- src/LmcUser/Factory/UserHydrator.php | 17 ++------- .../AdapterChainServiceFactoryTest.php | 4 +- 12 files changed, 82 insertions(+), 174 deletions(-) diff --git a/src/LmcUser/Authentication/Adapter/AdapterChainServiceFactory.php b/src/LmcUser/Authentication/Adapter/AdapterChainServiceFactory.php index 50f5c86..f42e5a0 100644 --- a/src/LmcUser/Authentication/Adapter/AdapterChainServiceFactory.php +++ b/src/LmcUser/Authentication/Adapter/AdapterChainServiceFactory.php @@ -2,27 +2,27 @@ namespace LmcUser\Authentication\Adapter; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Laminas\ServiceManager\Exception\ServiceNotCreatedException; -use Laminas\ServiceManager\Exception\ServiceNotFoundException; -use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; -use LmcUser\Authentication\Adapter\AdapterChain; -use LmcUser\Options\ModuleOptions; +use Laminas\ServiceManager\Factory\FactoryInterface; use LmcUser\Authentication\Adapter\Exception\OptionsNotFoundException; +use LmcUser\Options\ModuleOptions; class AdapterChainServiceFactory implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $chain = new AdapterChain(); - $chain->setEventManager($serviceLocator->get('EventManager')); + $chain->setEventManager($container->get('EventManager')); - $options = $this->getOptions($serviceLocator); + $options = $this->getOptions($container); //iterate and attach multiple adapters and events if offered foreach ($options->getAuthAdapters() as $priority => $adapterName) { - $adapter = $serviceLocator->get($adapterName); + $adapter = $container->get($adapterName); if (is_callable(array($adapter, 'authenticate'))) { $chain->getEventManager()->attach('authenticate', array($adapter, 'authenticate'), $priority); @@ -41,11 +41,6 @@ public function __invoke(ContainerInterface $serviceLocator, $requestedName, arr */ protected $options; - public function createService(ServiceLocatorInterface $serviceLocator) - { - $this->__invoke($serviceLocator, null); - } - /** * set options diff --git a/src/LmcUser/Factory/Authentication/Adapter/DbFactory.php b/src/LmcUser/Factory/Authentication/Adapter/DbFactory.php index 64c7dfe..5768918 100644 --- a/src/LmcUser/Factory/Authentication/Adapter/DbFactory.php +++ b/src/LmcUser/Factory/Authentication/Adapter/DbFactory.php @@ -4,27 +4,19 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Authentication\Adapter\Db; class DbFactory implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $db = new Db(); - $db->setServiceManager($serviceLocator); + $db->setServiceManager($container); return $db; } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->__invoke($serviceLocator, null); - } } diff --git a/src/LmcUser/Factory/Authentication/Storage/DbFactory.php b/src/LmcUser/Factory/Authentication/Storage/DbFactory.php index 338b7a6..0eb9588 100644 --- a/src/LmcUser/Factory/Authentication/Storage/DbFactory.php +++ b/src/LmcUser/Factory/Authentication/Storage/DbFactory.php @@ -4,27 +4,19 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Authentication\Storage\Db; class DbFactory implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $db = new Db(); - $db->setServiceManager($serviceLocator); + $db->setServiceManager($container); return $db; } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->__invoke($serviceLocator, null); - } } diff --git a/src/LmcUser/Factory/AuthenticationService.php b/src/LmcUser/Factory/AuthenticationService.php index 5f32c36..9a52ced 100644 --- a/src/LmcUser/Factory/AuthenticationService.php +++ b/src/LmcUser/Factory/AuthenticationService.php @@ -4,26 +4,18 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; class AuthenticationService implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) - { - return new \Laminas\Authentication\AuthenticationService( - $serviceLocator->get('LmcUser\Authentication\Storage\Db'), - $serviceLocator->get('LmcUser\Authentication\Adapter\AdapterChain') - ); - } - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - return $this->__invoke($serviceLocator, null); + return new \Laminas\Authentication\AuthenticationService( + $container->get('LmcUser\Authentication\Storage\Db'), + $container->get('LmcUser\Authentication\Adapter\AdapterChain') + ); } } diff --git a/src/LmcUser/Factory/Controller/Plugin/LmcUserAuthentication.php b/src/LmcUser/Factory/Controller/Plugin/LmcUserAuthentication.php index c8b1e23..292e28f 100644 --- a/src/LmcUser/Factory/Controller/Plugin/LmcUserAuthentication.php +++ b/src/LmcUser/Factory/Controller/Plugin/LmcUserAuthentication.php @@ -4,15 +4,18 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Controller; class LmcUserAuthentication implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $authService = $serviceLocator->get('lmcuser_auth_service'); - $authAdapter = $serviceLocator->get('LmcUser\Authentication\Adapter\AdapterChain'); + $authService = $container->get('lmcuser_auth_service'); + $authAdapter = $container->get('LmcUser\Authentication\Adapter\AdapterChain'); $controllerPlugin = new Controller\Plugin\LmcUserAuthentication; $controllerPlugin->setAuthService($authService); @@ -20,17 +23,4 @@ public function __invoke(ContainerInterface $serviceLocator, $requestedName, arr return $controllerPlugin; } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceManager - * @return mixed - */ - public function createService(ServiceLocatorInterface $serviceManager) - { - $serviceLocator = $serviceManager->getServiceLocator(); - - return $this->__invoke($serviceLocator, null); - } } diff --git a/src/LmcUser/Factory/Controller/RedirectCallbackFactory.php b/src/LmcUser/Factory/Controller/RedirectCallbackFactory.php index 8106b8f..bf095e2 100644 --- a/src/LmcUser/Factory/Controller/RedirectCallbackFactory.php +++ b/src/LmcUser/Factory/Controller/RedirectCallbackFactory.php @@ -6,34 +6,26 @@ use Laminas\Mvc\Application; use Laminas\Router\RouteInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Controller\RedirectCallback; use LmcUser\Options\ModuleOptions; class RedirectCallbackFactory implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var RouteInterface $router */ - $router = $serviceLocator->get('Router'); + $router = $container->get('Router'); /* @var Application $application */ - $application = $serviceLocator->get('Application'); + $application = $container->get('Application'); /* @var ModuleOptions $options */ - $options = $serviceLocator->get('lmcuser_module_options'); + $options = $container->get('lmcuser_module_options'); return new RedirectCallback($application, $router, $options); } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->__invoke($serviceLocator, null); - } } diff --git a/src/LmcUser/Factory/Controller/UserControllerFactory.php b/src/LmcUser/Factory/Controller/UserControllerFactory.php index b60dd5f..9c9292b 100644 --- a/src/LmcUser/Factory/Controller/UserControllerFactory.php +++ b/src/LmcUser/Factory/Controller/UserControllerFactory.php @@ -3,44 +3,32 @@ namespace LmcUser\Factory\Controller; use Interop\Container\ContainerInterface; -use Laminas\Mvc\Controller\ControllerManager; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Controller\RedirectCallback; use LmcUser\Controller\UserController; class UserControllerFactory implements FactoryInterface { - public function __invoke(ContainerInterface $serviceManager, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var RedirectCallback $redirectCallback */ - $redirectCallback = $serviceManager->get('lmcuser_redirect_callback'); + $redirectCallback = $container->get('lmcuser_redirect_callback'); /* @var UserController $controller */ $controller = new UserController($redirectCallback); - $controller->setServiceLocator($serviceManager); + $controller->setServiceLocator($container); - $controller->setChangeEmailForm($serviceManager->get('lmcuser_change_email_form')); - $controller->setOptions($serviceManager->get('lmcuser_module_options')); - $controller->setChangePasswordForm($serviceManager->get('lmcuser_change_password_form')); - $controller->setLoginForm($serviceManager->get('lmcuser_login_form')); - $controller->setRegisterForm($serviceManager->get('lmcuser_register_form')); - $controller->setUserService($serviceManager->get('lmcuser_user_service')); + $controller->setChangeEmailForm($container->get('lmcuser_change_email_form')); + $controller->setOptions($container->get('lmcuser_module_options')); + $controller->setChangePasswordForm($container->get('lmcuser_change_password_form')); + $controller->setLoginForm($container->get('lmcuser_login_form')); + $controller->setRegisterForm($container->get('lmcuser_register_form')); + $controller->setUserService($container->get('lmcuser_user_service')); return $controller; } - - /** - * Create service - * - * @param ServiceLocatorInterface $controllerManager - * @return mixed - */ - public function createService(ServiceLocatorInterface $controllerManager) - { - /* @var ControllerManager $controllerManager*/ - $serviceManager = $controllerManager->getServiceLocator(); - - return $this->__invoke($serviceManager, null); - } } diff --git a/src/LmcUser/Factory/Mapper/User.php b/src/LmcUser/Factory/Mapper/User.php index 0b31c5b..e6655f9 100644 --- a/src/LmcUser/Factory/Mapper/User.php +++ b/src/LmcUser/Factory/Mapper/User.php @@ -4,17 +4,20 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Mapper; use LmcUser\Options\ModuleOptions; class User implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /** @var ModuleOptions $options */ - $options = $serviceLocator->get('lmcuser_module_options'); - $dbAdapter = $serviceLocator->get('lmcuser_laminas_db_adapter'); + $options = $container->get('lmcuser_module_options'); + $dbAdapter = $container->get('lmcuser_laminas_db_adapter'); $entityClass = $options->getUserEntityClass(); $tableName = $options->getTableName(); @@ -27,15 +30,4 @@ public function __invoke(ContainerInterface $serviceLocator, $requestedName, arr return $mapper; } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->__invoke($serviceLocator, null); - } } diff --git a/src/LmcUser/Factory/Options/ModuleOptions.php b/src/LmcUser/Factory/Options/ModuleOptions.php index 98583ad..8eedce4 100644 --- a/src/LmcUser/Factory/Options/ModuleOptions.php +++ b/src/LmcUser/Factory/Options/ModuleOptions.php @@ -4,26 +4,18 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Options; class ModuleOptions implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) - { - $config = $serviceLocator->get('Config'); - - return new Options\ModuleOptions(isset($config['lmcuser']) ? $config['lmcuser'] : array()); - } - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - return $this->__invoke($serviceLocator, null); + $config = $container->get('Config'); + + return new Options\ModuleOptions(isset($config['lmcuser']) ? $config['lmcuser'] : array()); } } diff --git a/src/LmcUser/Factory/Service/UserFactory.php b/src/LmcUser/Factory/Service/UserFactory.php index b2de148..2232971 100644 --- a/src/LmcUser/Factory/Service/UserFactory.php +++ b/src/LmcUser/Factory/Service/UserFactory.php @@ -4,27 +4,19 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use LmcUser\Service\User; class UserFactory implements FactoryInterface { - public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) + /** + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $service = new User(); - $service->setServiceManager($serviceLocator); + $service->setServiceManager($container); return $service; } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->__invoke($serviceLocator, null); - } } diff --git a/src/LmcUser/Factory/UserHydrator.php b/src/LmcUser/Factory/UserHydrator.php index 21b108f..f5875cc 100644 --- a/src/LmcUser/Factory/UserHydrator.php +++ b/src/LmcUser/Factory/UserHydrator.php @@ -4,24 +4,15 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; class UserHydrator implements FactoryInterface { - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) - { - return new \Laminas\Hydrator\ClassMethods(); - } - - /** - * Create service - * - * @param ServiceLocatorInterface $serviceLocator - * @return mixed + * {@inheritDoc} + * @see \Laminas\ServiceManager\Factory\FactoryInterface::__invoke() */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - return $this->__invoke($serviceLocator, null); + return new \Laminas\Hydrator\ClassMethods(); } } diff --git a/tests/LmcUserTest/Authentication/Adapter/AdapterChainServiceFactoryTest.php b/tests/LmcUserTest/Authentication/Adapter/AdapterChainServiceFactoryTest.php index c1dfde7..d8af262 100644 --- a/tests/LmcUserTest/Authentication/Adapter/AdapterChainServiceFactoryTest.php +++ b/tests/LmcUserTest/Authentication/Adapter/AdapterChainServiceFactoryTest.php @@ -61,9 +61,9 @@ protected function setUp() } /** - * @covers \LmcUser\Authentication\Adapter\AdapterChainServiceFactory::createService + * @covers \LmcUser\Authentication\Adapter\AdapterChainServiceFactory::__invoke */ - public function testCreateService() + public function testInvoke() { $adapter = array( 'adapter1'=> $this->getMock(