ScriptAdapter::onModeScriptCallbackArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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