Completed
Pull Request — master (#459)
by Vytautas
14:01
created

EntityResolverFactory::getOptionsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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