Completed
Push — master ( c11d86...d758ed )
by Gianluca
13:50
created

EventManagerFactory::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 1
Bugs 0 Features 0
Metric Value
c 1
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 DoctrineModule\Service;
21
22
use Interop\Container\ContainerInterface;
23
use InvalidArgumentException;
24
use Doctrine\Common\EventManager;
25
use Doctrine\Common\EventSubscriber;
26
use Zend\ServiceManager\ServiceLocatorInterface;
27
28
/**
29
 * Factory responsible for creating EventManager instances
30
 */
31
class EventManagerFactory extends AbstractFactory
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36 4
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
37
    {
38
        /** @var $options \DoctrineModule\Options\EventManager */
39 4
        $options      = $this->getOptions($container, 'eventmanager');
40 4
        $eventManager = new EventManager();
41
42 4
        foreach ($options->getSubscribers() as $subscriberName) {
43 4
            $subscriber = $subscriberName;
44
45 4
            if (is_string($subscriber)) {
46 3
                if ($container->has($subscriber)) {
47 1
                    $subscriber = $container->get($subscriber);
48 3
                } elseif (class_exists($subscriber)) {
49 1
                    $subscriber = new $subscriber();
50 1
                }
51 3
            }
52
53 4
            if ($subscriber instanceof EventSubscriber) {
54 3
                $eventManager->addEventSubscriber($subscriber);
55 3
                continue;
56
            }
57
58 1
            $subscriberType = is_object($subscriberName) ? get_class($subscriberName) : $subscriberName;
59
60 1
            throw new InvalidArgumentException(
61 1
                sprintf(
62
                    'Invalid event subscriber "%s" given, must be a service name, '
63 1
                    . 'class name or an instance implementing Doctrine\Common\EventSubscriber',
64 1
                    is_string($subscriberType) ? $subscriberType : gettype($subscriberType)
65 1
                )
66 1
            );
67 3
        }
68
69 3
        return $eventManager;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 4
    public function createService(ServiceLocatorInterface $container)
76
    {
77 4
        return $this($container, EventManager::class);
78
    }
79
80
    /**
81
     * Get the class name of the options associated with this factory.
82
     *
83
     * @return string
84
     */
85 4
    public function getOptionsClass()
86
    {
87 4
        return 'DoctrineModule\Options\EventManager';
88
    }
89
}
90