AbstractContextInitializer::getSubscribedEvents()   A
last analyzed

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
abstract class AbstractContextInitializer
18
    implements
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
19
        FixtureInitializerInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 8 found
Loading history...
20
        ImportFixtureEventListener,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 8 found
Loading history...
21
        PurgeFixtureEventListener
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 8 found
Loading history...
22
{
23
24
    /**
25
     * Данные контекста
26
     *
27
     * @var array|null
28
     */
29
    protected $contextData;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getSubscribedEvents()
35
    {
36
        return [
37
            ImportFixtureEventListener::IMPORT,
38
            PurgeFixtureEventListener::PURGE,
39
        ];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    abstract public function purge(FixtureEvent $event);
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    abstract public function import(FixtureEvent $event);
51
52
    /**
53
     * Возвращает данные контекста
54
     *
55
     * @return array
56
     * @throws \Nnx\DoctrineFixtureModule\FixtureInitializer\Exception\RuntimeException
57
     */
58
    public function getContextData()
59
    {
60
        if (null === $this->contextData) {
61
            $errMsg = 'Context not specified';
62
            throw new Exception\RuntimeException($errMsg);
63
        }
64
        return $this->contextData;
65
    }
66
67
    /**
68
     * Устанавливает данные контекста
69
     *
70
     * @param array|null $contextData
71
     *
72
     * @return $this
73
     */
74
    public function setContextData(array $contextData)
75
    {
76
        $this->contextData = $contextData;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Возвращает значение параметра из контекста
83
     *
84
     * @param $name
85
     *
86
     * @return mixed
87
     * @throws \Nnx\DoctrineFixtureModule\FixtureInitializer\Exception\RuntimeException
88
     */
89
    public function getContextParam($name)
90
    {
91
        $context = $this->getContextData();
92
        if (!array_key_exists($name, $context)) {
93
            $errMsg = sprintf('Param %s not found in context', $name);
94
            throw new Exception\RuntimeException($errMsg);
95
        }
96
97
        return $context[$name];
98
    }
99
}
100