Completed
Pull Request — master (#59)
by
unknown
06:25 queued 03:32
created

Widget   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 7
dl 0
loc 262
ccs 63
cts 70
cp 0.9
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 1
A getFmlManialink() 0 4 1
A setScriptData() 0 4 1
A getXml() 0 10 1
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
A addDictionaryInformation() 0 12 3
B getDictionaryInformation() 0 16 5
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 FML\Controls\Frame;
8
use FML\Controls\Label;
9
use FML\Elements\Dico;
10
use FML\Elements\Format;
11
use FML\Script\Features\ToggleInterface;
12
use FML\Types\Container;
13
use FML\Types\Renderable;
14
15
class Widget extends Manialink implements Container
16
{
17
18
    /** @var  ManiaScript */
19
    protected $scriptData;
20
21
    /** @var Translations */
22
    protected $translationHelper;
23
24
    /** @var \FML\ManiaLink */
25
    protected $manialink;
26
27
    /** @var Dico */
28
    protected $dictionary;
29
30
    /** @var Label */
31
    protected $closeButton;
32
33
    /** @var Frame */
34
    protected $contentFrame;
35
36
    /** @var Frame */
37
    protected $windowFrame;
38
39 3
    public function __construct(
40
        Group $group,
41
        Translations $translationHelper,
42
        $name,
43
        $sizeX,
44
        $sizeY,
45
        $posX = null,
46
        $posY = null
47
    ) {
48 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
49
50 3
        $this->translationHelper = $translationHelper;
51
52
        // Manialink containing everything
53 3
        $this->manialink = new \FML\ManiaLink();
54 3
        $this->manialink->setId($this->getId())
55 3
            ->setName($name)
56 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
57
58 3
        $this->dictionary = new Dico();
59 3
        $this->manialink->setDico($this->dictionary);
60
61 3
        $windowFrame = new Frame('Window');
62 3
        $windowFrame->setPosition($posX, $posY);
63 3
        $this->manialink->addChild($windowFrame);
64
65
        // Frame to handle the content of the window.
66 3
        $this->contentFrame = new Frame();
67 3
        $this->contentFrame->setPosition(0, 0);
68 3
        $this->contentFrame->setSize($sizeX, $sizeY);
69 3
        $windowFrame->addChild($this->contentFrame);
70
71 3
        $this->windowFrame = $windowFrame;
72 3
    }
73
74
    /**
75
     * @return \FML\ManiaLink
76
     */
77
    public function getFmlManialink()
78
    {
79
        return $this->manialink;
80
    }
81
82
    /**
83
     * sets scripts data
84
     *
85
     * @param ManiaScript $script
86
     */
87
    public function setScriptData(ManiaScript $script)
88
    {
89
        $this->scriptData = $script->__toString();
0 ignored issues
show
Documentation Bug introduced by
It seems like $script->__toString() of type string is incompatible with the declared type object<eXpansion\Framewo...\Model\Gui\ManiaScript> of property $scriptData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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