Completed
Pull Request — master (#213)
by
unknown
07:58
created

CustomUi::onApplicationStop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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 Maniaplanet\DedicatedServer\Connection;
9
10
/**
11
 * Class CustomUi
12
 *
13
 * @package eXpansion\Bundle\CustomUi\Plugins
14
 */
15
class CustomUi implements StatusAwarePluginInterface
16
{
17
    /** @var Connection */
18
    protected $connection;
19
20
    /**@var Group */
21
    protected $allPlayers;
22
23
    /** @var string[] */
24
    protected $uiProperties;
25
26
    /** @var string */
27
    protected $setPropertiesScriptEvent;
28
29
    /**@var WidgetFactory[] */
30
    protected $customWidgets;
31
32
    /**
33
     * CustomUi constructor.
34
     *
35
     * @param Connection $connection
36
     * @param Group $allPlayers
37
     * @param string[] $uiProperties
38
     * @param string $setPropertiesScriptEvent
39
     * @param WidgetFactory[] $customWidgets
40
     */
41
    public function __construct(
42
        Connection $connection,
43
        Group $allPlayers,
44
        array $uiProperties,
45
        string $setPropertiesScriptEvent,
46
        array $customWidgets
47
    ) {
48
        $this->connection = $connection;
49
        $this->allPlayers = $allPlayers;
50
        $this->uiProperties = $uiProperties;
51
        $this->setPropertiesScriptEvent = $setPropertiesScriptEvent;
52
        $this->customWidgets = $customWidgets;
53
    }
54
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function setStatus($status)
60
    {
61
        if ($status) {
62
            $xml = new \SimpleXMLElement('<ui_properties/>');
63
            foreach ($this->uiProperties as $property => $propertyDetails) {
64
                $this->configureUiProperty($xml->addChild($property), $propertyDetails);
0 ignored issues
show
Documentation introduced by
$propertyDetails is of type string, but the function expects a array.

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...
65
            }
66
            $this->connection->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...
67
68
            if (empty($this->customWidgets)) {
69
                return;
70
            }
71
            foreach ($this->customWidgets as $widget) {
72
                $widget->create($this->allPlayers);
73
            }
74
        } else {
75
            if (empty($this->customWidgets)) {
76
                return;
77
            }
78
79
            foreach ($this->customWidgets as $widget) {
80
                $widget->destroy($this->allPlayers);
81
            }
82
        }
83
    }
84
85
    /**
86
     * @param \SimpleXMLElement $element
87
     * @param array $elementProperties
88
     */
89
    protected function configureUiProperty(\SimpleXMLElement $element, $elementProperties)
90
    {
91
        foreach ($elementProperties as $property => $value) {
92
            if (in_array($property, ['visible', 'alt_visible'])) {
93
                $value = $value ? 'true' : 'false';
94
            }
95
96
            $element->addAttribute($property, $value);
97
        }
98
    }
99
}
100