DoctrineAdapterAbstractFactory   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 22
c 8
b 1
f 1
lcom 0
cbo 4
dl 0
loc 96
ccs 44
cts 44
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D canCreateServiceWithName() 0 27 10
B createServiceWithName() 0 28 5
C getObjectManagerService() 0 22 7
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\Persistence\Doctrine\DataMapper\Manager;
9
10
use Doctrine\Common\Persistence\ObjectManager;
11
use Thorr\Persistence\Doctrine\DataMapper\DoctrineAdapter;
12
use Zend\ServiceManager\AbstractFactoryInterface;
13
use Zend\ServiceManager\AbstractPluginManager;
14
use Zend\ServiceManager\Exception;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
17
class DoctrineAdapterAbstractFactory implements AbstractFactoryInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 11
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
23
    {
24 11
        $serviceManager = $serviceLocator instanceof AbstractPluginManager ?
25 11
            $serviceLocator->getServiceLocator() : $serviceLocator;
26
27 11
        $config = $serviceManager->get('config');
28
29 11
        if (! isset($config['thorr_persistence_dmm']['doctrine']['adapters'][$requestedName])) {
30 4
            return false;
31
        }
32
33 7
        $dataMapperSpec = $config['thorr_persistence_dmm']['doctrine']['adapters'][$requestedName];
34
35 7
        if (! is_array($dataMapperSpec) && ! is_string($dataMapperSpec)) {
36 1
            return false;
37
        }
38
39 6
        if (is_string($dataMapperSpec) && ! class_exists($dataMapperSpec)) {
40 1
            return false;
41
        }
42
43 5
        if (is_array($dataMapperSpec) && (! isset($dataMapperSpec['class']) || ! class_exists($dataMapperSpec['class']))) {
44 2
            return false;
45
        }
46
47 3
        return true;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @throws Exception\InvalidServiceNameException
54
     */
55 5
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
56
    {
57 5
        $serviceManager = $serviceLocator instanceof AbstractPluginManager ?
58 5
            $serviceLocator->getServiceLocator() : $serviceLocator;
59
60 5
        $config = $serviceManager->get('config');
61
62 5
        $dataMapperSpec      = $config['thorr_persistence_dmm']['doctrine']['adapters'][$requestedName];
63 5
        $dataMapperClassName = is_string($dataMapperSpec) ? $dataMapperSpec : $dataMapperSpec['class'];
64
65 5
        $entityClass = array_search($requestedName, $config['thorr_persistence_dmm']['entity_data_mapper_map']);
66
67 5
        $objectManagerServiceName = isset($dataMapperSpec['object_manager']) ? $dataMapperSpec['object_manager'] : null;
68
69 5
        $objectManager = $this->getObjectManagerService($serviceManager, $objectManagerServiceName);
70
71 4
        $instance = new $dataMapperClassName($entityClass, $objectManager);
72
73 4
        if (! $instance instanceof DoctrineAdapter) {
74 1
            throw new Exception\ServiceNotCreatedException(sprintf(
75 1
                'Invalid data mapper type: "%s" is not a subtype of "%s"',
76 1
                $dataMapperClassName,
77
                DoctrineAdapter::class
78 1
            ));
79
        }
80
81 3
        return $instance;
82
    }
83
84
    /**
85
     * @param ServiceLocatorInterface $serviceLocator
86
     * @param string                  $objectManagerServiceName
87
     *
88
     * @return ObjectManager
89
     */
90 5
    protected function getObjectManagerService(ServiceLocatorInterface $serviceLocator, $objectManagerServiceName)
91
    {
92 5
        $config = $serviceLocator->get('config');
93
94 5
        if (! $objectManagerServiceName && isset($config['thorr_persistence_dmm']['doctrine']['object_manager'])) {
95 4
            $objectManagerServiceName = $config['thorr_persistence_dmm']['doctrine']['object_manager'];
96 4
        }
97
98 5
        $objectManager = $objectManagerServiceName && $serviceLocator->has($objectManagerServiceName) ?
99 5
            $serviceLocator->get($objectManagerServiceName)
100 5
            : null;
101
102 5
        if (! $objectManager instanceof ObjectManager) {
103 1
            throw new Exception\InvalidServiceNameException(sprintf(
104 1
                'Invalid object manager type: "%s" is not a subtype of "%s"',
105 1
                is_object($objectManager) ? get_class($objectManager) : gettype($objectManager),
106
                ObjectManager::class
107 1
            ));
108
        }
109
110 4
        return $objectManager;
111
    }
112
}
113