Completed
Pull Request — master (#50)
by De Cramer
02:53
created

DataProviderManager::deletePlugin()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 4
eloc 6
nc 4
nop 2
crap 4
1
<?php
2
3
namespace eXpansion\Framework\Core\Services;
4
5
use eXpansion\Framework\Core\DataProviders\AbstractDataProvider;
6
use eXpansion\Framework\Core\Exceptions\DataProvider\UncompatibleException;
7
use eXpansion\Framework\Core\Model\ProviderListner;
8
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
9
use eXpansion\Framework\Core\Storage\GameDataStorage;
10
use oliverde8\AssociativeArraySimplified\AssociativeArray;
11
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Class DataProviderManager handles all the data providers.
17
 *
18
 * @TODO handle gamemode change.
19
 *
20
 * @package eXpansion\Framework\Core\Services
21
 */
22
class DataProviderManager
23
{
24
    /** For compatibility with every title/mode/script */
25
    const COMPATIBLE_ALL = "ALL";
26
27
    /** @var int[][][][]  List of providers by compatibility. */
28
    protected $providersByCompatibility = [];
29
30
    /** @var string[] Name of the provider for a service Id. */
31
    protected $providerById = [];
32
33
    /** @var string[] Interface a plugin needs extend/implement to be used by a provider. */
34
    protected $providerInterfaces = [];
35
36
    /** @var ProviderListner[][] Providers that listen a certain event. */
37
    protected $providerListeners = [];
38
39
    /** @var ProviderListner[][] Enabled providers that listen to certain events. */
40
    protected $enabledProviderListeners = [];
41
42
    /** @var ContainerInterface */
43
    protected $container;
44
45
    /** @var GameDataStorage  */
46
    protected $gameDataStorage;
47
48
    /**
49
     * DataProviderManager constructor.
50
     *
51
     * @param ContainerInterface $container
52
     */
53 44
    public function __construct(ContainerInterface $container, GameDataStorage $gameDataStorage)
54
    {
55 44
        $this->container = $container;
56 44
        $this->gameDataStorage = $gameDataStorage;
57 44
    }
58
59
    /**
60
     * Initialize all the providers properly.
61
     */
62 1
    public function init(PluginManager $pluginManager)
63
    {
64 1
        $this->reset($pluginManager);
65
    }
66
67 1
    public function reset(PluginManager $pluginManager)
68
    {
69 1
        $title = $this->gameDataStorage->getVersion();
70 1
        $mode = $this->gameDataStorage->getGameModeCode();
71
        $script = $this->gameDataStorage->getGameInfos()->scriptName;
72
73
        foreach ($this->providersByCompatibility as $provider => $data) {
74
75
            $providerId = $this->getCompatibleProviderId($provider, $title, $mode, $script);
0 ignored issues
show
Documentation introduced by
$title is of type object<Maniaplanet\Dedic...ver\Structures\Version>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
            if ($providerId) {
78
                $providerService = $this->container->get($providerId);
79
80
                if ($pluginManager->isPluginEnabled($providerId)) {
81
                    foreach ($this->providerListeners[$providerId] as $listener) {
82
                        $this->enabledProviderListeners[$listener->getEventName()][] = [
83
                            $providerService,
84
                            $listener->getMethod()
85
                        ];
86
                    }
87
                }
88
            }
89
        }
90
    }
91
92
    /**
93
     * Register a provider.
94
     *
95
     * @param string $id
96
     * @param string $provider
97
     * @param string $interface
98
     * @param string[][] $compatibilities
99
     * @param string[] $listeners
100
     */
101 44
    public function registerDataProvider($id, $provider, $interface, $compatibilities, $listeners)
102
    {
103 44
        foreach ($compatibilities as $compatibility) {
104 44
            $this->providersByCompatibility[$provider][$compatibility['title']][$compatibility['mode']][$compatibility['script']] = $id;
105
        }
106
107 44
        foreach ($listeners as $eventName => $method) {
108 44
            $this->providerListeners[$id][] = new ProviderListner($eventName, $provider, $method);
109
        }
110 44
        $this->providerInterfaces[$provider] = $interface;
111 44
        $this->providerById[$id] = $provider;
112 44
    }
113
114
    /**
115
     * Checl of a provider is compatible
116
     *
117
     * @param string $provider
118
     * @param string $title
119
     * @param string $mode
120
     * @param string $script
121
     *
122
     * @return bool
123
     */
124 1
    public function isProviderCompatible($provider, $title, $mode, $script)
125
    {
126 1
        return !is_null($this->getCompatibleProviderId($provider, $title, $mode, $script));
127
    }
128
129
    /**
130
     * @param string $provider
131
     * @param string $title
132
     * @param string $mode
133
     * @param string $script
134
     *
135
     * @return string|null
136
     */
137 3
    public function getCompatibleProviderId($provider, $title, $mode, $script)
138
    {
139
        $parameters = [
140 3
            [$provider, $title, $mode, $script],
141 3
            [$provider, $title, $mode, self::COMPATIBLE_ALL],
142 3
            [$provider, $title, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
143 3
            [$provider, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
144
        ];
145
146 3
        foreach ($parameters as $parameter) {
147 3
            $id = AssociativeArray::getFromKey($this->providersByCompatibility, $parameter);
148 3
            if (!is_null($id)) {
149 3
                return $id;
150
            }
151
        }
152
153 1
        return null;
154
    }
155
156
    /**
157
     * Register a plugin to the DataProviders.
158
     *
159
     * @param string $provider The provider to register the plugin to.
160
     * @param string $pluginId The id of the plugin to be registered.
161
     * @param string $title The title to register it for.
162
     * @param string $mode The mode to register it for.
163
     * @param string $script The script to register it for.
164
     *
165
     * @throws UncompatibleException
166
     */
167 2
    public function registerPlugin($provider, $pluginId, $title, $mode, $script)
168
    {
169
        /** @var AbstractDataProvider $providerService */
170 2
        $providerService = $this->container->get($this->getCompatibleProviderId($provider, $title, $mode, $script));
171 2
        $pluginService = $this->container->get($pluginId);
172 2
        $interface = $this->providerInterfaces[$provider];
173
174 2
        if ($pluginService instanceof $interface) {
175 1
            $this->deletePlugin($provider, $pluginId);
176 1
            $providerService->registerPlugin($pluginId, $pluginService);
177
        } else {
178 1
            throw new UncompatibleException("Plugin $pluginId isn't compatible with $provider. Should be instance of $interface");
179
        }
180 1
    }
181
182
    /**
183
     * Provider to delete a plugin from.
184
     *
185
     * @param $provider
186
     * @param $pluginId
187
     *
188
     */
189 1
    public function deletePlugin($provider, $pluginId)
190
    {
191 1
        foreach ($this->providersByCompatibility[$provider] as $titleProviders) {
192 1
            foreach ($titleProviders as $modeProviders) {
193 1
                foreach ($modeProviders as $providerId) {
194 1
                    $providerService = $this->container->get($providerId);
195 1
                    $providerService->deletePlugin($pluginId);
196
                }
197
            }
198
        }
199 1
    }
200
201
    /**
202
     * Dispatch event to the data providers.
203
     *
204
     * @param $eventName
205
     * @param $params
206
     */
207 24
    public function dispatch($eventName, $params)
208
    {
209 24
        if (isset($this->enabledProviderListeners[$eventName])) {
210
            foreach ($this->enabledProviderListeners[$eventName] as $callback) {
211
                call_user_func_array($callback, $params);
212
            }
213
        }
214 24
    }
215
}
216