Completed
Push — dev ( 854686...1d55ed )
by Андрей
04:48
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine-fixture-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\DoctrineFixtureModule\FixtureInitializer;
7
8
use Doctrine\Fixture\Event\FixtureEvent;
9
use Doctrine\Fixture\Event\ImportFixtureEventListener;
10
use Doctrine\Fixture\Event\PurgeFixtureEventListener;
11
12
/**
13
 * Class ObjectManagerNameInitializer
14
 *
15
 * @package Nnx\DoctrineFixtureModule\FixtureInitializer
16
 */
17
class ObjectManagerNameInitializer extends AbstractContextInitializer
18
{
19
20
    /**
21
     * Имя параметра из контекста, значение которого содержит имя ObjectManager'a
22
     *
23
     * @var string
24
     */
25
    const OBJECT_MANAGER_NAME = 'objectManagerName';
26
27
    /**
28
     * ObjectManagerNameInitializer constructor.
29
     *
30
     * @param array $contextData
31
     */
32
    public function __construct(array $contextData = [])
33
    {
34
        $this->setContextData($contextData);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getSubscribedEvents()
41
    {
42
        return [
43
            ImportFixtureEventListener::IMPORT,
44
            PurgeFixtureEventListener::PURGE,
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     * @throws \Nnx\DoctrineFixtureModule\FixtureInitializer\Exception\RuntimeException
51
     */
52
    public function purge(FixtureEvent $event)
53
    {
54
        $this->injected($event);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     * @throws \Nnx\DoctrineFixtureModule\FixtureInitializer\Exception\RuntimeException
60
     */
61
    public function import(FixtureEvent $event)
62
    {
63
        $this->injected($event);
64
    }
65
66
    /**
67
     * Устанавливает зависимости в фикстуру
68
     *
69
     * @param FixtureEvent $event
70
     *
71
     * @throws \Nnx\DoctrineFixtureModule\FixtureInitializer\Exception\RuntimeException
72
     */
73
    protected function injected(FixtureEvent $event)
74
    {
75
        $fixture = $event->getFixture();
76
77
        if (! ($fixture instanceof ObjectManagerNameAwareInterface)) {
78
            return;
79
        }
80
81
        $objectManagerName = $this->getContextParam(static::OBJECT_MANAGER_NAME);
82
        $fixture->setObjectManagerName($objectManagerName);
83
    }
84
}
85