Completed
Pull Request — master (#309)
by De Cramer
14:06
created

VoteWidgetFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 9
dl 0
loc 18
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Plugins\Gui\Widget;
4
5
use eXpansion\Bundle\VoteManager\Services\VoteService;
6
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
7
use eXpansion\Framework\Core\Model\Gui\Widget;
8
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
9
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
10
use eXpansion\Framework\Gui\Builders\WidgetBackground;
11
use eXpansion\Framework\Gui\Components\Animation;
12
use eXpansion\Framework\Gui\Components\Button;
13
use eXpansion\Framework\Gui\Components\Label;
14
use eXpansion\Framework\Gui\Ui\Factory;
15
use FML\Controls\Frame;
16
use FML\Controls\Quad;
17
use FML\Script\ScriptLabel;
18
19
class VoteWidgetFactory extends WidgetFactory
20
{
21
22
    const x = 90;
23
    const y = 20;
24
25
    /** @var Label */
26
    protected $label;
27
    /**
28
     * @var VoteService
29
     */
30
    private $voteService;
31
32
    /**
33
     * @var UpdateVoteWidgetFactory
34
     */
35
    private $updateVoteWidgetFactory;
36
37
38
    /***
39
     * MenuFactory constructor.
40
     *
41
     * @param                         $name
42
     * @param                         $sizeX
43
     * @param                         $sizeY
44
     * @param null                    $posX
45
     * @param null                    $posY
46
     * @param WidgetFactoryContext    $context
47
     * @param Factory                 $uiFactory
48
     * @param VoteService             $voteService
49
     * @param UpdateVoteWidgetFactory $updateVoteWidgetFactory
50
     */
51
    public function __construct(
52
        $name,
53
        $sizeX,
54
        $sizeY,
55
        $posX,
56
        $posY,
57
        WidgetFactoryContext $context,
58
        Factory $uiFactory,
59
        VoteService $voteService,
60
        UpdateVoteWidgetFactory $updateVoteWidgetFactory
61
62
    ) {
63
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
64
65
        $this->uiFactory = $uiFactory;
66
        $this->voteService = $voteService;
67
        $this->updateVoteWidgetFactory = $updateVoteWidgetFactory;
68
    }
69
70
    /**
71
     * @param Widget|ManialinkInterface $manialink
72
     */
73
    protected function createContent(ManialinkInterface $manialink)
74
    {
75
        $frame = Frame::create();
76
        $frame->setScale(0.8);
77
        $manialink->addChild($frame);
78
79
        $label = $this->uiFactory->createLabel($this->voteService->getCurrentVote()->getQuestion(), Label::TYPE_HEADER);
80
        $label->setTextColor("fff")
81
            ->setPosition(self::x / 2, -1)
82
            ->setTextSize(4)
83
            ->setAlign("center", "top")
84
            ->setTranslate(true);
85
        $this->label = $label;
86
        $frame->addChild($this->label);
87
88
        $btnPosition = -9;
89
        $btn = $this->uiFactory->createButton(" F1", Button::TYPE_DEFAULT);
90
        $btn->setSize(18, 6)->setPosition(1, $btnPosition)
91
            ->setId("ButtonYes")
92
            ->setBackgroundColor("0f09");
93
        $btn->setAction(
94
            $this->actionFactory->createManialinkAction($manialink, [$this, "callbackYes"], null)
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
95
        );
96
        $frame->addChild($btn);
97
98
        $btn = $this->uiFactory->createButton(" F2", Button::TYPE_DEFAULT);
99
        $btn->setSize(18, 6)->setPosition(self::x - 19, $btnPosition)
100
            ->setId("ButtonNo")
101
            ->setBackgroundColor("f009");
102
        $btn->setAction(
103
            $this->actionFactory->createManialinkAction($manialink, [$this, "callbackNo"], null)
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
104
        );
105
        $frame->addChild($btn);
106
107
        $quad = Quad::create();
108
        $quad->setAlign("center", "center2");
109
        $quad->setSize(0.5, 9);
110
        $quad->setPosition(self::x / 2, $btnPosition - 3)
111
            ->setBackgroundColor("fff");
112
        $frame->addChild($quad);
113
114
        $quad = Quad::create("yes");
115
        $quad->setAlign("left", "top");
116
        $quad->setSize((self::x - 20 * 2) / 2, 6);
117
        $quad->setPosition(20, $btnPosition)
118
            ->setBackgroundColor("0f09");
119
        $frame->addChild($quad);
120
121
        $quad = Quad::create("no");
122
        $quad->setAlign("right", "top");
123
        $quad->setSize((self::x - 20 * 2) / 2, 6);
124
        $quad->setPosition(self::x - 20, $btnPosition)
125
            ->setBackgroundColor("0000");
126
        $frame->addChild($quad);
127
128
        $animation = $this->uiFactory->createAnimation();
129
        $manialink->addChild($animation);
130
131
        $quad = Quad::create("timer");
132
        $quad->setSize(self::x - 4, 1);
133
        $quad->setPosition(2, -self::y + 1)
134
            ->setAlign("left", "bottom")
135
            ->setBackgroundColor("fffa");
136
        $frame->addChild($quad);
137
138
        $animation->addAnimation($quad,
139
            "size='0 1'",
140
            $this->voteService->getCurrentVote()->getDuration() * 1000,
141
            0,
142
            Animation::Linear);
143
144
145
        $bg = new WidgetBackground(90, 20);
146
        $frame->addChild($bg);
147
148
        $x = self::x;
149
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::KeyPress,
150
            <<<EOL
151
            
152
            if (Event.KeyName == "F1") {
153
                TriggerButtonClick("ButtonYes");                            
154
            }
155
            
156
            if (Event.KeyName == "F2") {
157
               TriggerButtonClick("ButtonNo");                                 
158
            }
159
EOL
160
        );
161
162
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit,
163
164
            <<<EOL
165
            declare Real SizeX = 1. * ($x - 40) ;
166
            declare CMlQuad BgYes = (Page.GetFirstChild("yes") as CMlQuad);
167
            declare CMlQuad BgNo = (Page.GetFirstChild("no") as CMlQuad);
168
            {$this->updateVoteWidgetFactory->getScriptInitialization()}                                                                       
169
EOL
170
        );
171
172
        $variable = $this->updateVoteWidgetFactory->getVariable('VoteUpdater')->getVariableName();
173
        $onChange = $this->updateVoteWidgetFactory->getScriptOnChange(/** @lang text */
174
            <<<EOL
175
            
176
               declare Integer Yes = {$variable}["yes"];
177
               declare Integer No = {$variable}["no"];
178
               declare Real Total = 1. * (Yes + No);
179
               
180
               if (Total > 0) {
181
                    declare Real Ratio = 1. * (Yes / Total);
182
                    AnimMgr.Add(BgYes, "<elem size=\""^( SizeX * Ratio )^" 6\" />", 250, CAnimManager::EAnimManagerEasing::QuadInOut);               
183
                    AnimMgr.Add(BgNo, "<elem size=\""^( SizeX  * (1. - Ratio) )^" 6\" />", 250, CAnimManager::EAnimManagerEasing::QuadInOut);                                         
184
               }
185
               
186
EOL
187
        );
188
189
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop, $onChange);
190
    }
191
192
    public function callbackYes($manialink, $login, $entries, $args)
193
    {
194
        if ($this->voteService->getCurrentVote()) {
195
            $this->voteService->getCurrentVote()->castYes($login);
196
        }
197
198
    }
199
200
    public function callbackNo($manialink, $login, $entries, $args)
201
    {
202
        if ($this->voteService->getCurrentVote()) {
203
            $this->voteService->getCurrentVote()->castNo($login);
204
        }
205
    }
206
207
}