Code Duplication    Length = 62-63 lines in 6 locations

src/EventDispatcher/EventDispatcherAbstractFactory.php 1 location

@@ 21-82 (lines=62) @@
18
 *
19
 * @package Nnx\JmsSerializerModule\EventDispatcher
20
 */
21
class EventDispatcherAbstractFactory implements AbstractFactoryInterface
22
{
23
    /**
24
     * @inheritdoc
25
     *
26
     * @param ServiceLocatorInterface $serviceLocator
27
     * @param                         $name
28
     * @param                         $requestedName
29
     *
30
     * @return bool|void
31
     */
32
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
33
    {
34
        return 0 === strpos($requestedName, 'nnxJmsSerializer.eventDispatchers.');
35
    }
36
37
    /**
38
     * @inheritdoc
39
     *
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param                         $name
42
     * @param                         $requestedName
43
     *
44
     * @return EventDispatcherInterface
45
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
46
     * @throws \Nnx\JmsSerializerModule\EventDispatcher\Exception\RuntimeException
47
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
52
53
        $eventDispatcherName = substr($requestedName, 34);
54
55
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
56
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
58
        /** @var ModuleOptions $moduleOptions */
59
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
60
61
        $eventDispatcherConfig = $moduleOptions->getEventDispatcher($eventDispatcherName);
62
63
        $name = $eventDispatcherConfig->getName();
64
        $options = $eventDispatcherConfig->getOptions();
65
66
        $eventDispatcher =  $serviceLocator->get(
67
            $name,
68
            $options
69
        );
70
71
        if (!$eventDispatcher instanceof EventDispatcherInterface) {
72
            $errMsg = sprintf(
73
                'Object constructor of type %s is invalid; must implement %s',
74
                (is_object($eventDispatcher) ? get_class($eventDispatcher) : gettype($eventDispatcher)),
75
                EventDispatcherInterface::class
76
            );
77
            throw new Exception\RuntimeException($errMsg);
78
        }
79
80
        return $eventDispatcher;
81
    }
82
}
83

src/HandlerRegistry/HandlerRegistryAbstractFactory.php 1 location

@@ 20-82 (lines=63) @@
17
 *
18
 * @package Nnx\JmsSerializerModule\HandlerRegistry
19
 */
20
class HandlerRegistryAbstractFactory implements AbstractFactoryInterface
21
{
22
    /**
23
     * @inheritdoc
24
     *
25
     * @param ServiceLocatorInterface $serviceLocator
26
     * @param                         $name
27
     * @param                         $requestedName
28
     *
29
     * @return bool|void
30
     */
31
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
32
    {
33
        return 0 === strpos($requestedName, 'nnxJmsSerializer.handlerRegistry.');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     *
39
     * @param ServiceLocatorInterface $serviceLocator
40
     * @param                         $name
41
     * @param                         $requestedName
42
     *
43
     * @return HandlerRegistryInterface
44
     * @throws \Nnx\JmsSerializerModule\HandlerRegistry\Exception\RuntimeException
45
     * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException
46
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
47
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
52
53
        $handlerRegistryName = substr($requestedName, 33);
54
55
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
56
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
58
        /** @var ModuleOptions $moduleOptions */
59
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
60
61
        $handlerRegistryConfig = $moduleOptions->getHandlerRegistry($handlerRegistryName);
62
63
        $name = $handlerRegistryConfig->getName();
64
        $options = $handlerRegistryConfig->getOptions();
65
66
        $handlerRegistry =  $serviceLocator->get(
67
            $name,
68
            $options
69
        );
70
71
        if (!$handlerRegistry instanceof HandlerRegistryInterface) {
72
            $errMsg = sprintf(
73
                'Handler registry of type %s is invalid; must implement %s',
74
                (is_object($handlerRegistry) ? get_class($handlerRegistry) : gettype($handlerRegistry)),
75
                HandlerRegistryInterface::class
76
            );
77
            throw new Exception\RuntimeException($errMsg);
78
        }
79
80
        return $handlerRegistry;
81
    }
82
}
83

src/MetadataDriver/FileLocatorAbstractFactory.php 1 location

@@ 22-83 (lines=62) @@
19
 *
20
 * @package Nnx\JmsSerializerModule\MetadataDriver
21
 */
22
class FileLocatorAbstractFactory implements AbstractFactoryInterface
23
{
24
    /**
25
     * @inheritdoc
26
     *
27
     * @param ServiceLocatorInterface $serviceLocator
28
     * @param                         $name
29
     * @param                         $requestedName
30
     *
31
     * @return bool|void
32
     */
33
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
34
    {
35
        return 0 === strpos($requestedName, 'nnxJmsSerializer.fileLocators.');
36
    }
37
38
    /**
39
     * @inheritdoc
40
     *
41
     * @param ServiceLocatorInterface $serviceLocator
42
     * @param                         $name
43
     * @param                         $requestedName
44
     *
45
     * @return AdapterInterface
46
     * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException
47
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
48
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
49
     */
50
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
51
    {
52
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
53
54
        $fileLocatorName = substr($requestedName, 30);
55
56
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
57
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
58
59
        /** @var ModuleOptions $moduleOptions */
60
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
61
62
        $fileLocatorConfig = $moduleOptions->getFileLocator($fileLocatorName);
63
64
        $name = $fileLocatorConfig->getName();
65
        $options = $fileLocatorConfig->getOptions();
66
67
        $fileLocator =  $serviceLocator->get(
68
            $name,
69
            $options
70
        );
71
72
        if (!$fileLocator instanceof FileLocatorInterface) {
73
            $errMsg = sprintf(
74
                'File locator of type %s is invalid; must implement %s',
75
                (is_object($fileLocator) ? get_class($fileLocator) : gettype($fileLocator)),
76
                FileLocatorInterface::class
77
            );
78
            throw new Exception\RuntimeException($errMsg);
79
        }
80
81
        return $fileLocator;
82
    }
83
}
84

src/NamingStrategy/NamingStrategyAbstractFactory.php 1 location

@@ 20-81 (lines=62) @@
17
 *
18
 * @package Nnx\JmsSerializerModule\NamingStrategy
19
 */
20
class NamingStrategyAbstractFactory implements AbstractFactoryInterface
21
{
22
    /**
23
     * @inheritdoc
24
     *
25
     * @param ServiceLocatorInterface $serviceLocator
26
     * @param                         $name
27
     * @param                         $requestedName
28
     *
29
     * @return bool|void
30
     */
31
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
32
    {
33
        return 0 === strpos($requestedName, 'nnxJmsSerializer.namingStrategies.');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     *
39
     * @param ServiceLocatorInterface $serviceLocator
40
     * @param                         $name
41
     * @param                         $requestedName
42
     *
43
     * @return PropertyNamingStrategyInterface
44
     * @throws \Nnx\JmsSerializerModule\NamingStrategy\Exception\RuntimeException
45
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
46
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
47
     */
48
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
49
    {
50
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
51
52
        $strategyName = substr($requestedName, 34);
53
54
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
55
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
56
57
        /** @var ModuleOptions $moduleOptions */
58
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
59
60
        $strategyConfig = $moduleOptions->getNamingStrategy($strategyName);
61
62
        $name = $strategyConfig->getName();
63
        $options = $strategyConfig->getOptions();
64
65
        $strategy =  $serviceLocator->get(
66
            $name,
67
            $options
68
        );
69
70
        if (!$strategy instanceof PropertyNamingStrategyInterface) {
71
            $errMsg = sprintf(
72
                'Naming strategy of type %s is invalid; must implement %s',
73
                (is_object($strategy) ? get_class($strategy) : gettype($strategy)),
74
                PropertyNamingStrategyInterface::class
75
            );
76
            throw new Exception\RuntimeException($errMsg);
77
        }
78
79
        return $strategy;
80
    }
81
}
82

src/ObjectConstructor/ObjectConstructorAbstractFactory.php 1 location

@@ 21-82 (lines=62) @@
18
 *
19
 * @package Nnx\JmsSerializerModule\ObjectConstructor
20
 */
21
class ObjectConstructorAbstractFactory implements AbstractFactoryInterface
22
{
23
    /**
24
     * @inheritdoc
25
     *
26
     * @param ServiceLocatorInterface $serviceLocator
27
     * @param                         $name
28
     * @param                         $requestedName
29
     *
30
     * @return bool|void
31
     */
32
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
33
    {
34
        return 0 === strpos($requestedName, 'nnxJmsSerializer.objectConstructor.');
35
    }
36
37
    /**
38
     * @inheritdoc
39
     *
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param                         $name
42
     * @param                         $requestedName
43
     *
44
     * @return ObjectConstructorInterface
45
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
46
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
47
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
52
53
        $objectConstructorName = substr($requestedName, 35);
54
55
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
56
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
58
        /** @var ModuleOptions $moduleOptions */
59
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
60
61
        $objectConstructorConfig = $moduleOptions->getObjectConstructor($objectConstructorName);
62
63
        $name = $objectConstructorConfig->getName();
64
        $options = $objectConstructorConfig->getOptions();
65
66
        $objectConstructor =  $serviceLocator->get(
67
            $name,
68
            $options
69
        );
70
71
        if (!$objectConstructor instanceof ObjectConstructorInterface) {
72
            $errMsg = sprintf(
73
                'Object constructor of type %s is invalid; must implement %s',
74
                (is_object($objectConstructor) ? get_class($objectConstructor) : gettype($objectConstructor)),
75
                ObjectConstructorInterface::class
76
            );
77
            throw new Exception\RuntimeException($errMsg);
78
        }
79
80
        return $objectConstructor;
81
    }
82
}
83

src/Visitor/VisitorsAbstractFactory.php 1 location

@@ 21-83 (lines=63) @@
18
 *
19
 * @package Nnx\JmsSerializerModule\Visitor
20
 */
21
class VisitorsAbstractFactory implements AbstractFactoryInterface
22
{
23
    /**
24
     * @inheritdoc
25
     *
26
     * @param ServiceLocatorInterface $serviceLocator
27
     * @param                         $name
28
     * @param                         $requestedName
29
     *
30
     * @return bool|void
31
     */
32
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
33
    {
34
        return 0 === strpos($requestedName, 'nnxJmsSerializer.visitors.');
35
    }
36
37
    /**
38
     * @inheritdoc
39
     *
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param                         $name
42
     * @param                         $requestedName
43
     *
44
     * @return ObjectConstructorInterface
45
     * @throws \Nnx\JmsSerializerModule\Visitor\Exception\RuntimeException
46
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
47
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
48
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
49
     */
50
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
51
    {
52
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
53
54
        $visitorName = substr($requestedName, 26);
55
56
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
57
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
58
59
        /** @var ModuleOptions $moduleOptions */
60
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
61
62
        $visitorConfig = $moduleOptions->getVisitor($visitorName);
63
64
        $name = $visitorConfig->getName();
65
        $options = $visitorConfig->getOptions();
66
67
        $visitor =  $serviceLocator->get(
68
            $name,
69
            $options
70
        );
71
72
        if (!$visitor instanceof VisitorInterface) {
73
            $errMsg = sprintf(
74
                'VisitorInterface of type %s is invalid; must implement %s',
75
                (is_object($visitor) ? get_class($visitor) : gettype($visitor)),
76
                VisitorInterface::class
77
            );
78
            throw new Exception\RuntimeException($errMsg);
79
        }
80
81
        return $visitor;
82
    }
83
}
84