Completed
Pull Request — master (#479)
by Michał
04:19
created

EntityResolverFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 93.33%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 4
c 6
b 0
f 0
lcom 0
cbo 4
dl 0
loc 38
ccs 14
cts 15
cp 0.9333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 22 3
A getOptionsClass() 0 4 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 as OptionsEntityResolver;
27
use Interop\Container\ContainerInterface;
28
29
class EntityResolverFactory extends AbstractFactory
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34 56
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
35
    {
36
        /** @var $options OptionsEntityResolver */
37 56
        $options      = $this->getOptions($container, 'entity_resolver');
38 56
        $eventManager = $container->get($options->getEventManager());
39 56
        $resolvers    = $options->getResolvers();
40
41 56
        $targetEntityListener = new ResolveTargetEntityListener();
42
43 56
        foreach ($resolvers as $oldEntity => $newEntity) {
44 56
            $targetEntityListener->addResolveTargetEntity($oldEntity, $newEntity, []);
45 56
        }
46
47
        // Starting from Doctrine ORM 2.5, the listener implements EventSubscriber
48 56
        if ($targetEntityListener instanceof EventSubscriber) {
49 56
            $eventManager->addEventSubscriber($targetEntityListener);
50 56
        } else {
51
            $eventManager->addEventListener(Events::loadClassMetadata, $targetEntityListener);
52
        }
53
54 56
        return $eventManager;
55
    }
56
57
    /**
58
     * Get the class name of the options associated with this factory.
59
     *
60
     * @return string
61
     */
62 56
    public function getOptionsClass()
63
    {
64 56
        return OptionsEntityResolver::class;
65
    }
66
}
67