Completed
Push — master ( 947184...ad3618 )
by
unknown
9s
created

Dispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 48.15%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 89
ccs 13
cts 27
cp 0.4815
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 13 2
A reset() 0 5 1
A addEventProcessor() 0 4 1
A dispatch() 0 12 3
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
    protected $temporizedEvents = [];
31
32
    /**
33
     * Dispatcher constructor.
34
     *
35
     * @param DataProviderManager $dataProviderManager
36
     * @param PluginManager $pluginManager
37
     */
38 150
    public function __construct(DataProviderManager $dataProviderManager, PluginManager $pluginManager)
39
    {
40 150
        $this->dataProviderManager = $dataProviderManager;
41 150
        $this->pluginManager = $pluginManager;
42 150
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function init(Connection $connection)
48
    {
49
        $map = $connection->getCurrentMapInfo();
50
51
        $this->pluginManager->init($map);
52
        $this->dataProviderManager->reset($this->pluginManager, $map);
53
        $this->isInitialized = true;
54
55
        foreach ($this->temporizedEvents as $temporizedEvent) {
56
            $this->dispatch($temporizedEvent['event'], $temporizedEvent['params']);
57
        }
58
        $this->temporizedEvents = [];
59
    }
60
61
    /**
62
     * Reset when game mode changes.
63
     */
64
    /**
65
     * Reset the dispatcher elements when game mode changes.
66
     *
67
     * @param Map $map Current map.
68
     *
69
     * @return void
70
     *
71
     * @throws
72
     */
73
    public function reset(Map $map)
74
    {
75
        $this->pluginManager->reset($map);
76
        $this->dataProviderManager->reset($this->pluginManager, $map);
77
    }
78
79
    /**
80
     * Add a processor of events.
81
     *
82
     * @param EventProcessorInterface $eventProcessor
83
     */
84 150
    public function addEventProcessor(EventProcessorInterface $eventProcessor)
85
    {
86 150
        $this->eventProcessors[] = $eventProcessor;
87 150
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 29
    public function dispatch($event, $params)
93
    {
94 29
        foreach ($this->eventProcessors as $eventProcessor) {
95 29
            $eventProcessor->dispatch($event, $params);
96
        }
97
98 29
        if ($this->isInitialized) {
99
            $this->dataProviderManager->dispatch($event, $params);
100
        } else {
101 29
            $this->temporizedEvents[] = ['event' => $event, 'params' => $params];
102
        }
103
    }
104
}