Completed
Push — dev ( ade326...e1b211 )
by
unknown
03:23
created

Dispatcher::dispatch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
cc 3
eloc 5
nc 4
nop 2
crap 3.0416
1
<?php
2
3
namespace eXpansion\Framework\Core\Services\Application;
4
5
use eXpansion\Framework\Core\Services\DataProviderManager;
6
use eXpansion\Framework\Core\Services\PluginManager;
7
8
/**
9
 * Class Dispatcher, dispatches events to the Data Providers.
10
 *
11
 * @package eXpansion\Framework\Core\Services\Application
12
 * @author Oliver de Cramer
13
 */
14
class Dispatcher implements DispatcherInterface
15
{
16
    /** @var DataProviderManager  */
17
    protected $dataProviderManager;
18
19
    /** @var PluginManager */
20
    protected $pluginManager;
21
22
    /** @var EventProcessorInterface[] */
23
    protected $eventProcessors = [];
24
25
    /** @var bool  */
26
    protected $isInitialized = false;
27
28
    /**
29
     * Dispatcher constructor.
30
     *
31
     * @param DataProviderManager $dataProviderManager
32
     * @param PluginManager $pluginManager
33
     */
34 48
    public function __construct(DataProviderManager $dataProviderManager, PluginManager $pluginManager)
35
    {
36 48
        $this->dataProviderManager = $dataProviderManager;
37 48
        $this->pluginManager = $pluginManager;
38 48
    }
39
40
    /**
41
     * Init.
42
     */
43
    public function init()
44
    {
45
        $this->pluginManager->init();
46
        $this->dataProviderManager->init($this->pluginManager);
47
48
        $this->isInitialized = true;
49
    }
50
51
    /**
52
     * Reset when game mode changes.
53
     */
54
    public function reset()
55
    {
56
57
    }
58
59
    /**
60
     * Add a processor of events.
61
     *
62
     * @param EventProcessorInterface $eventProcessor
63
     */
64 48
    public function addEventProcesseor(EventProcessorInterface $eventProcessor)
65
    {
66 48
        $this->eventProcessors[] = $eventProcessor;
67 48
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 24
    public function dispatch($event, $params)
73
    {
74 24
        foreach ($this->eventProcessors as $eventProcessor) {
75 24
            $eventProcessor->dispatch($event, $params);
76
        }
77
78 24
        if ($this->isInitialized) {
79
            $this->dataProviderManager->dispatch($event, $params);
80
        }
81
    }
82
}