Completed
Push — master ( d80cf4...f87515 )
by Gianluca
04:15
created

canCreateServiceWithName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModule\ServiceFactory;
21
22
use Zend\ServiceManager\AbstractFactoryInterface;
23
use Zend\ServiceManager\Exception\ServiceNotFoundException;
24
use Zend\ServiceManager\ServiceLocatorInterface;
25
26
/**
27
 * Abstract service factory capable of instantiating services whose names match the
28
 * pattern <code>doctrine.$serviceType.$serviceName</doctrine>
29
 *
30
 * @license MIT
31
 * @link    http://www.doctrine-project.org/
32
 * @author  Marco Pivetta <[email protected]>
33
 */
34
class AbstractDoctrineServiceFactory implements AbstractFactoryInterface
35
{
36
    /**
37
     * {@inheritDoc}
38
     */
39 30
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
40
    {
41 30
        return false !== $this->getFactoryMapping($serviceLocator, $requestedName);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 1
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
48
    {
49 1
        $mappings = $this->getFactoryMapping($serviceLocator, $requestedName);
50
51 1
        if (! $mappings) {
52
            throw new ServiceNotFoundException();
53
        }
54
55 1
        $factoryClass = $mappings['factoryClass'];
56
        /* @var $factory \DoctrineModule\Service\AbstractFactory */
57 1
        $factory = new $factoryClass($mappings['serviceName']);
58
59 1
        return $factory->createService($serviceLocator);
60
    }
61
62
    /**
63
     * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
64
     * @param string                                       $name
65
     *
66
     * @return bool|array
67
     */
68 30
    private function getFactoryMapping(ServiceLocatorInterface $serviceLocator, $name)
69
    {
70 30
        $matches = array();
71
72 30
        if (!preg_match(
73 30
            '/^doctrine\.((?<mappingType>orm|odm)\.|)(?<serviceType>[a-z0-9_]+)\.(?<serviceName>[a-z0-9_]+)$/',
74 30
            $name,
75
            $matches
76 30
        )) {
77 10
            return false;
78
        }
79
80 20
        $config      = $serviceLocator->get('Config');
81 20
        $mappingType = $matches['mappingType'];
82 20
        $serviceType = $matches['serviceType'];
83 20
        $serviceName = $matches['serviceName'];
84
85 20
        if ($mappingType == '') {
86 20
            if (! isset($config['doctrine_factories'][$serviceType]) ||
87 18
                 ! isset($config['doctrine'][$serviceType][$serviceName])
88 20
            ) {
89 4
                return false;
90
            }
91
92
            return array(
93 16
                'serviceType'  => $serviceType,
94 16
                'serviceName'  => $serviceName,
95 16
                'factoryClass' => $config['doctrine_factories'][$serviceType],
96 16
            );
97
        } else {
98
            if (! isset($config['doctrine_factories'][$mappingType]) ||
99
                 ! isset($config['doctrine_factories'][$mappingType][$serviceType]) ||
100
                 ! isset($config['doctrine'][$mappingType][$serviceType][$serviceName])
101
            ) {
102
                return false;
103
            }
104
            return array(
105
                'serviceType'  => $serviceType,
106
                'serviceName'  => $serviceName,
107
                'factoryClass' => $config['doctrine_factories'][$mappingType][$serviceType],
108
            );
109
        }
110
    }
111
}
112