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