Completed
Pull Request — master (#129)
by Tim
24:26
created

EmitterFactory::createEmitter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Events\EmitterFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Events;
22
23
use League\Event\Emitter;
24
use League\Event\EmitterInterface;
25
use TechDivision\Import\ConfigurationInterface;
26
use Symfony\Component\DependencyInjection\TaggedContainerInterface;
27
28
/**
29
 * A factory implementation to create a new event emitter instance.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import
35
 * @link      http://www.techdivision.com
36
 */
37
class EmitterFactory
38
{
39
40
    /**
41
     * The configuration instance.
42
     *
43
     * @var \TechDivision\Import\ConfigurationInterface
44
     */
45
    protected $configuration;
46
47
    /**
48
     * The DI container builder instance.
49
     *
50
     * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface
51
     */
52
    protected $container;
53
54
    /**
55
     * The constructor to initialize the instance.
56
     *
57
     * @param \TechDivision\Import\ConfigurationInterface                     $configuration The configuration instance
58
     * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container     The container instance
59
     */
60
    public function __construct(ConfigurationInterface $configuration, TaggedContainerInterface $container)
61
    {
62
        $this->container = $container;
63
        $this->configuration = $configuration;
64
    }
65
66
    /**
67
     * The factory method that creates a new emitter instance.
68
     *
69
     * @return void
70
     */
71
    public function createEmitter()
72
    {
73
74
        // initialize the event emitter
75
        $emitter = new Emitter();
76
77
        // load, initialize and add the configured listeners to the emitter
78
        foreach ($this->configuration->getListeners() as $listeners) {
79
            $this->prepareListeners($emitter, $listeners);
80
        }
81
82
        // return the initialized emitter instance
83
        return $emitter;
84
    }
85
86
    /**
87
     * Prepare the listeners defined in the system configuration.
88
     *
89
     * @param \League\Event\EmitterInterface $emitter   The event emitter to prepare the listeners for
90
     * @param array                          $listeners The array with the listeners
91
     * @param string                         $eventName The actual event name
92
     *
93
     * @return void
94
     */
95
    protected function prepareListeners(EmitterInterface $emitter, array $listeners, $eventName = null)
96
    {
97
98
        // iterate over the array with listeners and prepare them
99
        foreach ($listeners as $key => $listener) {
100
            // we have to initialize the event name only on the first level
101
            if ($eventName == null) {
102
                $eventName = $key;
103
            }
104
            // query whether or not we've an subarray or not
105
            if (is_array($listener)) {
106
                $this->prepareListeners($emitter, $listener, $eventName);
107
            } else {
108
                $emitter->addListener($eventName, $this->container->get($listener));
109
            }
110
        }
111
    }
112
}
113