ScriptAdapter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 47
rs 10
c 0
b 0
f 0
ccs 0
cts 19
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onModeScriptCallbackArray() 0 5 1
A parseParameters() 0 12 3
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins;
4
5
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyScript;
6
use eXpansion\Framework\Core\Services\Application\Dispatcher;
7
8
/**
9
 * Class ScriptAdapter
10
 *
11
 * @package eXpansion\Framework\Core\Plugins;
12
 * @author  oliver de Cramer <[email protected]>
13
 */
14
class ScriptAdapter implements ListenerInterfaceMpLegacyScript
15
{
16
    /** @var  Dispatcher */
17
    protected $dispatcher;
18
19
    /**
20
     * ScriptAdapter constructor.
21
     *
22
     * @param Dispatcher $dispatcher
23
     */
24
    public function __construct(Dispatcher $dispatcher)
25
    {
26
        $this->dispatcher = $dispatcher;
27
    }
28
29
    /**
30
     * @param string $eventName  Name of the event.
31
     * @param mixed  $parameters Parameters.
32
     *
33
     * @return mixed
34
     */
35
    public function onModeScriptCallbackArray($eventName, $parameters)
36
    {
37
        $parameters = $this->parseParameters($parameters);
38
        $this->dispatcher->dispatch($eventName, [$parameters]);
39
    }
40
41
    /**
42
     * Parse parameters that are usually in json.
43
     *
44
     * @param $parameters
45
     *
46
     * @return string|array
47
     */
48
    protected function parseParameters($parameters)
49
    {
50
        if (isset($parameters[0])) {
51
            $params = json_decode($parameters[0], true);
52
            if ($params) {
53
                return $params;
54
            }
55
        }
56
57
        // If json couldn't be encoded return string as it was
58
        return $parameters;
59
    }
60
}
61