Completed
Pull Request — master (#527)
by
unknown
14:10 queued 08:39
created

EntityResolverFactory::createService()   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

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 DoctrineORMModule\Service;
21
22
use Doctrine\Common\EventSubscriber;
23
use Doctrine\ORM\Events;
24
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
25
use DoctrineModule\Service\AbstractFactory;
26
use DoctrineORMModule\Options\EntityResolver;
27
use Interop\Container\ContainerInterface;
28
use Zend\EventManager\EventManager;
29
use Zend\ServiceManager\ServiceLocatorInterface;
30
31
class EntityResolverFactory extends AbstractFactory
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36 72
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
37
    {
38
        /* @var $options EntityResolver */
39 72
        $options      = $this->getOptions($container, 'entity_resolver');
40 72
        $eventManager = $container->get($options->getEventManager());
41 72
        $resolvers    = $options->getResolvers();
42
43 72
        $targetEntityListener = new ResolveTargetEntityListener();
44
45 72
        foreach ($resolvers as $oldEntity => $newEntity) {
46 72
            $targetEntityListener->addResolveTargetEntity($oldEntity, $newEntity, []);
47 72
        }
48
49
        // Starting from Doctrine ORM 2.5, the listener implements EventSubscriber
50 72
        if ($targetEntityListener instanceof EventSubscriber) {
51 72
            $eventManager->addEventSubscriber($targetEntityListener);
52 72
        } else {
53
            $eventManager->addEventListener(Events::loadClassMetadata, $targetEntityListener);
54
        }
55
56 72
        return $eventManager;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 72
    public function createService(ServiceLocatorInterface $container)
63
    {
64 72
        return $this($container, EventManager::class);
65
    }
66
67
    /**
68
     * Get the class name of the options associated with this factory.
69
     *
70
     * @return string
71
     */
72 72
    public function getOptionsClass()
73
    {
74 72
        return EntityResolver::class;
75
    }
76
}
77