Completed
Pull Request — master (#123)
by De Cramer
12:37
created

Widget   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 91.03%

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 7
dl 0
loc 280
c 0
b 0
f 0
ccs 71
cts 78
cp 0.9103
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 40 1
A getFmlManialink() 0 4 1
A setScriptData() 0 4 1
A getXml() 0 6 1
A addDictionaryInformation() 0 12 3
C getDictionaryInformation() 0 27 7
A getChildren() 0 4 1
A addChild() 0 6 1
A add() 0 6 1
A addChildren() 0 6 1
A removeAllChildren() 0 6 1
A removeChildren() 0 6 1
A getFormat() 0 4 1
A setFormat() 0 6 1
A getContentFrame() 0 4 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui;
4
5
use eXpansion\Framework\Core\Helpers\Translations;
6
use eXpansion\Framework\Core\Model\UserGroups\Group;
7
use eXpansion\Framework\Gui\Components\uiButton;
8
use eXpansion\Framework\Gui\Components\uiCheckbox;
9
use eXpansion\Framework\Gui\Components\uiLabel;
10
use FML\Controls\Frame;
11
use FML\Controls\Label;
12
use FML\Elements\Dico;
13
use FML\Elements\Format;
14
use FML\Elements\SimpleScript;
15
use FML\Script\Features\ToggleInterface;
16
use FML\Script\Script;
17
use FML\Script\ScriptInclude;
18
use FML\Types\Container;
19
use FML\Types\Renderable;
20
21
class Widget extends Manialink implements Container
22
{
23
24
    /** @var  string */
25
    protected $scriptData;
26
27
    /** @var Translations */
28
    protected $translationHelper;
29
30
    /** @var \FML\ManiaLink */
31
    protected $manialink;
32
33
    /** @var Dico */
34
    protected $dictionary;
35
36
    /** @var Label */
37
    protected $closeButton;
38
39
    /** @var Frame */
40
    protected $contentFrame;
41
42
    /** @var Frame */
43
    protected $windowFrame;
44
45
    /** @var array[] */
46
    protected $cachedMessages = [];
47
48 3
    public function __construct(
49
        Group $group,
50
        Translations $translationHelper,
51
        $name,
52
        $sizeX,
53
        $sizeY,
54
        $posX = null,
55
        $posY = null
56
    ) {
57 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
58
59 3
        $this->translationHelper = $translationHelper;
60
61
        // Manialink containing everything
62 3
        $this->manialink = new \FML\ManiaLink();
63 3
        $this->manialink->setId($this->getId())
64 3
            ->setName($name)
65 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
66
67 3
        $this->dictionary = new Dico();
68 3
        $this->manialink->setDico($this->dictionary);
69
70 3
        $windowFrame = new Frame('Window');
71 3
        $windowFrame->setPosition($posX, $posY);
72 3
        $this->manialink->addChild($windowFrame);
73
74
        // Frame to handle the content of the window.
75 3
        $this->contentFrame = new Frame();
76 3
        $this->contentFrame->setPosition(0, 0);
77 3
        $this->contentFrame->setSize($sizeX, $sizeY);
78 3
        $windowFrame->addChild($this->contentFrame);
79
80 3
        $toggleInterfaceF9 = new ToggleInterface("F9");
0 ignored issues
show
Documentation introduced by
'F9' is of type string, but the function expects a object<FML\Controls\Control>|null.

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...
81 3
        $toggleInterfaceF9->setControl($windowFrame);
82
83 3
        $this->manialink->getScript()
84 3
            ->addFeature($toggleInterfaceF9);
85
86 3
        $this->windowFrame = $windowFrame;
87 3
    }
88
89
    /**
90
     * @return \FML\ManiaLink
91
     */
92
    public function getFmlManialink()
93
    {
94
        return $this->manialink;
95
    }
96
97
    /**
98
     * sets scripts data
99
     *
100
     * @param ManiaScript $script
101
     */
102
    public function setScriptData(ManiaScript $script)
103
    {
104
        $this->scriptData = $script->__toString();
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 1
    public function getXml()
111
    {
112 1
        $this->addDictionaryInformation();
113
114 1
        return $this->manialink->__toString();
115
    }
116
117
    /**
118
     * Add translations to dictionary.
119
     */
120 1
    protected function addDictionaryInformation()
121
    {
122 1
        $translations = [];
123 1
        $this->dictionary->removeAllEntries();
124 1
        $this->getDictionaryInformation($this->manialink, $translations);
125
126 1
        foreach ($translations as $msgId => $messages) {
127 1
            foreach ($messages as $message) {
128 1
                $this->dictionary->setEntry($message['Lang'], $msgId, htmlspecialchars($message['Text']));
129
            }
130
        }
131 1
    }
132
133
    /**
134
     * Recursive search all dome tree in order to find all translatable labels.
135
     *
136
     * @param Container|\FML\ManiaLink $control
137
     * @param $translations
138
     */
139 1
    protected function getDictionaryInformation($control, &$translations)
140
    {
141 1
        foreach ($control->getChildren() as $child) {
142 1
            if (($child instanceof Label
143 1
                    || $child instanceof uiLabel
144 1
                ) && $child->getTranslate()) {
145 1
                $id = $child->getTextId();
146
147 1
                if (!isset($this->cachedMessages[$id])) {
148 1
                    $textId = 'exp_'.md5($id);
149
150 1
                    $messages = $this->translationHelper->getTranslations($child->getTextId(), []);
151 1
                    $translations[$textId] = $messages;
152 1
                    $this->cachedMessages[$textId] = $messages;
153
154
                    // Replaces with text id that can be used in the xml.
155 1
                    $child->setTextId($textId);
156
                } else {
157 1
                    $translations[$id] = $this->cachedMessages[$id];
158
                }
159
            } else {
160 1
                if ($child instanceof Frame) {
161 1
                    $this->getDictionaryInformation($child, $translations);
162
                }
163
            }
164
        }
165 1
    }
166
167
    /**
168
     * Get the children
169
     *
170
     * @api
171
     * @return Renderable[]
172
     */
173 1
    public function getChildren()
174
    {
175 1
        return $this->contentFrame->getChildren();
176
    }
177
178
    /**
179
     * Add a new child
180
     *
181
     * @api
182
     *
183
     * @param Renderable $child Child Control to add
184
     *
185
     * @return static
186
     */
187 1
    public function addChild(Renderable $child)
188
    {
189 1
        $this->contentFrame->addChild($child);
190
191 1
        return $this;
192
    }
193
194
    /**
195
     * Add a new child
196
     *
197
     * @api
198
     *
199
     * @param Renderable $child Child Control to add
200
     *
201
     * @return static
202
     * @deprecated Use addChild()
203
     * @see        Container::addChild()
204
     */
205 1
    public function add(Renderable $child)
206
    {
207 1
        $this->contentFrame->addChild($child);
208
209 1
        return $this;
210
    }
211
212
    /**
213
     * Add new children
214
     *
215
     * @api
216
     *
217
     * @param Renderable[] $children Child Controls to add
218
     *
219
     * @return static
220
     */
221 1
    public function addChildren(array $children)
222
    {
223 1
        $this->contentFrame->addChildren($children);
224
225 1
        return $this;
226
    }
227
228
    /**
229
     * Remove all children
230
     *
231
     * @api
232
     * @return static
233
     */
234 1
    public function removeAllChildren()
235
    {
236 1
        $this->contentFrame->removeAllChildren();
237
238 1
        return $this;
239
    }
240
241
    /**
242
     * Remove all children
243
     *
244
     * @api
245
     * @return static
246
     * @deprecated Use removeAllChildren()
247
     * @see        Container::removeAllChildren()
248
     */
249 1
    public function removeChildren()
250
    {
251 1
        $this->contentFrame->removeAllChildren();
252
253 1
        return $this;
254
    }
255
256
    /**
257
     * Get the Format
258
     *
259
     * @api
260
     *
261
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
262
     *
263
     * @return Format
264
     * @deprecated Use Style
265
     * @see        Style
266
     */
267 1
    public function getFormat($createIfEmpty = true)
268
    {
269 1
        return $this->contentFrame->getFormat($createIfEmpty);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::getFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
270
    }
271
272
    /**
273
     * Set the Format
274
     *
275
     * @api
276
     *
277
     * @param Format $format New Format
278
     *
279
     * @return static
280
     * @deprecated Use Style
281
     * @see        Style
282
     *
283
     */
284 1
    public function setFormat(Format $format = null)
285
    {
286 1
        $this->contentFrame->setFormat($format);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::setFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
287
288 1
        return $this;
289
    }
290
291
    /**
292
     * @return Frame
293
     */
294
    public function getContentFrame()
295
    {
296
        return $this->contentFrame;
297
    }
298
299
300
}
301