CustomUi::setStatus()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 23
cp 0
rs 8.5866
c 0
b 0
f 0
cc 7
nc 9
nop 1
crap 56
1
<?php
2
3
namespace eXpansion\Bundle\CustomUi\Plugins;
4
5
use eXpansion\Framework\Core\Model\UserGroups\Group;
6
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
7
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
8
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
9
use Maniaplanet\DedicatedServer\Connection;
10
11
/**
12
 * Class CustomUi
13
 *
14
 * @package eXpansion\Bundle\CustomUi\Plugins
15
 */
16
class CustomUi implements StatusAwarePluginInterface
17
{
18
    /** @var Factory */
19
    protected $factory;
20
21
    /**@var Group */
22
    protected $allPlayers;
23
24
    /** @var string[] */
25
    protected $uiProperties;
26
27
    /** @var string */
28
    protected $setPropertiesScriptEvent;
29
30
    /**@var WidgetFactory[] */
31
    protected $customWidgets;
32
33
    /**
34
     * CustomUi constructor.
35
     *
36
     * @param Factory $factory
37
     * @param Group $allPlayers
38
     * @param array $uiProperties
39
     * @param string $setPropertiesScriptEvent
40
     * @param array $customWidgets
41
     */
42
    public function __construct(
43
        Factory $factory,
44
        Group $allPlayers,
45
        array $uiProperties,
46
        string $setPropertiesScriptEvent,
47
        array $customWidgets
48
    ) {
49
        $this->factory = $factory;
50
        $this->allPlayers = $allPlayers;
51
        $this->uiProperties = $uiProperties;
52
        $this->setPropertiesScriptEvent = $setPropertiesScriptEvent;
53
        $this->customWidgets = $customWidgets;
54
    }
55
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function setStatus($status)
61
    {
62
        if ($status) {
63
            $xml = new \SimpleXMLElement('<ui_properties/>');
64
            foreach ($this->uiProperties as $property => $propertyDetails) {
65
                $this->configureUiProperty($xml->addChild($property), $propertyDetails);
66
            }
67
            $this->factory->getConnection()->triggerModeScriptEvent($this->setPropertiesScriptEvent, [$xml->asXML()]);
0 ignored issues
show
Documentation introduced by
array($xml->asXML()) is of type array<integer,string|false,{"0":"string|false"}>, but the function expects a string|array<integer,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...
68
69
            if (empty($this->customWidgets)) {
70
                return;
71
            }
72
            foreach ($this->customWidgets as $widget) {
73
                $widget->create($this->allPlayers);
74
            }
75
        } else {
76
            if (empty($this->customWidgets)) {
77
                return;
78
            }
79
80
            foreach ($this->customWidgets as $widget) {
81
                $widget->destroy($this->allPlayers);
82
            }
83
        }
84
    }
85
86
    /**
87
     * @param \SimpleXMLElement $element
88
     * @param string $elementProperties
89
     */
90
    protected function configureUiProperty(\SimpleXMLElement $element, $elementProperties)
91
    {
92
        foreach ($elementProperties as $property => $value) {
0 ignored issues
show
Bug introduced by
The expression $elementProperties of type string is not traversable.
Loading history...
93
            if (in_array($property, ['visible', 'alt_visible'])) {
94
                $value = $value ? 'true' : 'false';
95
            }
96
97
            $element->addAttribute($property, $value);
98
        }
99
    }
100
}
101