Completed
Pull Request — master (#309)
by De Cramer
14:06
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
use Maniaplanet\DedicatedServer\Connection;
8
use Maniaplanet\DedicatedServer\Structures\Map;
9
10
/**
11
 * Class Dispatcher, dispatches events to the Data Providers.
12
 *
13
 * @package eXpansion\Framework\Core\Services\Application
14
 * @author Oliver de Cramer
15
 */
16
class Dispatcher implements DispatcherInterface
17
{
18
    /** @var DataProviderManager  */
19
    protected $dataProviderManager;
20
21
    /** @var PluginManager */
22
    protected $pluginManager;
23
24
    /** @var EventProcessorInterface[] */
25
    protected $eventProcessors = [];
26
27
    /** @var bool  */
28
    protected $isInitialized = false;
29
30
    /**
31
     * Dispatcher constructor.
32
     *
33
     * @param DataProviderManager $dataProviderManager
34
     * @param PluginManager $pluginManager
35
     */
36 164
    public function __construct(DataProviderManager $dataProviderManager, PluginManager $pluginManager)
37
    {
38 164
        $this->dataProviderManager = $dataProviderManager;
39 164
        $this->pluginManager = $pluginManager;
40 164
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function init(Connection $connection)
46
    {
47
        $map = $connection->getCurrentMapInfo();
48
49
        $this->pluginManager->init($map);
50
        $this->dataProviderManager->init($this->pluginManager, $map);
51
52
        $this->isInitialized = true;
53
    }
54
55
    /**
56
     * Reset when game mode changes.
57
     */
58
    /**
59
     * Reset the dispatcher elements when game mode changes.
60
     *
61
     * @param Map $map Current map.
62
     *
63
     * @return void
64
     *
65
     * @throws
66
     */
67
    public function reset(Map $map)
68
    {
69
        $this->pluginManager->reset($map);
70
        $this->dataProviderManager->reset($this->pluginManager, $map);
71
    }
72
73
    /**
74
     * Add a processor of events.
75
     *
76
     * @param EventProcessorInterface $eventProcessor
77
     */
78 164
    public function addEventProcessor(EventProcessorInterface $eventProcessor)
79
    {
80 164
        $this->eventProcessors[] = $eventProcessor;
81 164
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 30
    public function dispatch($event, $params)
87
    {
88 30
        foreach ($this->eventProcessors as $eventProcessor) {
89 30
            $eventProcessor->dispatch($event, $params);
90
        }
91
92 30
        if ($this->isInitialized) {
93
            $this->dataProviderManager->dispatch($event, $params);
94
        }
95
    }
96
}