Completed
Pull Request — master (#175)
by
unknown
05:05
created

ChatHelperWidget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 12
nc 1
nop 8
crap 2

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\CustomUi\Plugins\Gui;
4
5
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
6
use eXpansion\Framework\Core\Model\Gui\Widget;
7
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
8
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
9
use eXpansion\Framework\Core\Services\Application\Dispatcher;
10
use eXpansion\Framework\Core\Storage\PlayerStorage;
11
use FML\Script\ScriptLabel;
12
13
class ChatHelperWidget extends WidgetFactory
14
{
15
    /**
16
     * @var Dispatcher
17
     */
18
    private $dispatcher;
19
    /**
20
     * @var PlayerStorage
21
     */
22
    private $playerStorage;
23
24
    /**
25
     * ChatHelperWidget constructor.
26
     * @param                      $name
27
     * @param                      $sizeX
28
     * @param                      $sizeY
29
     * @param                      $posX
30
     * @param                      $posY
31
     * @param WidgetFactoryContext $context
32
     * @param Dispatcher           $dispatcher
33
     * @param PlayerStorage        $playerStorage
34
     */
35
    public function __construct(
36
        $name,
37
        $sizeX,
38
        $sizeY,
39
        $posX,
40
        $posY,
41
        WidgetFactoryContext $context,
42
        Dispatcher $dispatcher,
43
        PlayerStorage $playerStorage
44
    ) {
45
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
46
        $this->dispatcher = $dispatcher;
47
        $this->playerStorage = $playerStorage;
48
    }
49
50
51
    /**
52
     * @param ManialinkInterface|Widget $manialink
53
     */
54
    protected function createContent(ManialinkInterface $manialink)
55
    {
56
        parent::createContent($manialink);
57
58
        $entry = $this->uiFactory->createInput("publicChat", "", 90);
59
        $action = $this->actionFactory->createManialinkAction($manialink, [$this, "onChat"], []);
60
61
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::EntrySubmit, <<<eol
62
            if (Event.ControlId == "publicChat" ) {              
63
                declare CMlEntry Entry <=> (Page.GetFirstChild("publicChat") as CMlEntry);
64
                Entry.SetText(" ", False);  
65
            }
66
eol
67
        );
68
69
70
        $entry->setAction($action);
71
        $manialink->addChild($entry);
72
73
    }
74
75
    public function onChat($manialink, $login, $entries, $args)
76
    {
77
        $player = $this->playerStorage->getPlayerInfo($login);
78
        if (!empty($entries['publicChat'])) {
79
            $this->dispatcher->dispatch("PlayerChat", [
80
                $player->getPlayerId(),
81
                $login,
82
                $entries['publicChat'],
83
            ]);
84
        }
85
    }
86
87
}
88