Completed
Pull Request — master (#194)
by De Cramer
05:45
created

AbstractListWindow::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 22

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 26
loc 26
ccs 0
cts 10
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 13
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\Players\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\Helpers\ChatNotification;
7
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
9
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
10
use eXpansion\Framework\Core\Model\Gui\Window;
11
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
12
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
13
use eXpansion\Framework\Core\Storage\PlayerStorage;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\ChatCommandDataProvider;
15
use eXpansion\Framework\Gui\Components\uiButton;
16
use Maniaplanet\DedicatedServer\Connection;
17
18
abstract class AbstractListWindow extends GridWindowFactory
19
{
20
    /**
21
     * @var PlayerStorage
22
     */
23
    protected $playerStorage;
24
    /**
25
     * @var ChatCommandDataProvider
26
     */
27
    protected $chatCommandDataProvider;
28
    /**
29
     * @var Connection
30
     */
31
    protected $connection;
32
    /**
33
     * @var AdminGroups
34
     */
35
    protected $adminGroups;
36
37
    /**
38
     * @var ChatNotification
39
     */
40
    protected $chatNotification;
41
42
    /**
43
     * PlayersWindow constructor.
44
     * @param                         $name
45
     * @param                         $sizeX
46
     * @param                         $sizeY
47
     * @param null                    $posX
48
     * @param null                    $posY
49
     * @param WindowFactoryContext    $context
50
     * @param PlayerStorage           $playerStorage
51
     * @param DataCollectionFactory   $dataCollectionFactory
52
     * @param GridBuilderFactory      $gridBuilderFactory
53
     * @param ChatCommandDataProvider $chatCommandDataProvider
54
     * @param Connection              $connection
55
     * @param AdminGroups             $adminGroups
56
     * @param ChatNotification        $chatNotification
57
     */
58 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
        $name,
60
        $sizeX,
61
        $sizeY,
62
        $posX = null,
63
        $posY = null,
64
        WindowFactoryContext $context,
65
        PlayerStorage $playerStorage,
66
        DataCollectionFactory $dataCollectionFactory,
67
        GridBuilderFactory $gridBuilderFactory,
68
        ChatCommandDataProvider $chatCommandDataProvider,
69
        Connection $connection,
70
        AdminGroups $adminGroups,
71
        ChatNotification $chatNotification
72
73
    ) {
74
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
75
76
        $this->playerStorage = $playerStorage;
77
        $this->dataCollectionFactory = $dataCollectionFactory;
78
        $this->gridBuilderFactory = $gridBuilderFactory;
79
        $this->chatCommandDataProvider = $chatCommandDataProvider;
80
        $this->connection = $connection;
81
        $this->adminGroups = $adminGroups;
82
        $this->chatNotification = $chatNotification;
83
    }
84
85
    protected function createContent(
86
        ManialinkInterface $manialink
87
    ) {
88
        parent::createContent($manialink);
89
        $this->updateData($manialink);
90
    }
91
92
    protected function updateContent(
93
        ManialinkInterface $manialink
94
    ) {
95
        parent::updateContent($manialink);
96
    }
97
98
    /**
99
     * @param ManialinkInterface $manialink
100
     */
101
    protected function createGrid(
102
        ManialinkInterface $manialink
103
    ) {
104
105
        $selectButton = $this->uiFactory->createButton('expansion_players.gui.list.window.column.remove',
106
            uiButton::TYPE_DEFAULT)->setSize(10, 5)->setTranslate(true);
107
108
        $gridBuilder = $this->gridBuilderFactory->create();
109
        $gridBuilder->setManialink($manialink)
110
            ->setDataCollection($manialink->getData('dataCollection'))
111
            ->setManialinkFactory($this)
112
            ->addTextColumn(
113
                'login',
114
                'expansion_players.gui.players.window.column.login',
115
                4,
116
                true,
117
                false
118
119
            )
120
            ->addActionColumn('login', 'expansion_players.gui.list.window.column.remove',
121
                3, [$this, "playerCallback"], $selectButton);
122
123
124
        $manialink->setData('grid', $gridBuilder);
125
126
        $frame = $manialink->getContentFrame();
127
        $this->setGridSize($frame->getWidth(), $frame->getHeight() - 10);
128
129
        $this->setGridSize(60, 90);
130
        $this->setGridPosition(0, 0);
131
132
    }
133
134
    /**
135
     * Get list of players to display.
136
     *
137
     * @return array
138
     */
139
    abstract function getDataSet() : array;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
140
141
    /**
142
     * Execute action for player.
143
     *
144
     * @param $login
145
     *
146
     * @return mixed
147
     */
148
    abstract function executeForPlayer($login);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
149
150
151
    /**
152
     * @inheritdoc
153
     */
154
    public function updateData(
155
        $manialink
156
    ) {
157
        $dataset = $this->getDataSet();
158
159
        $listData = [];
160
        foreach ($dataset as $player) {
161
            $listData[] = ["login" => $player->login];
162
        }
163
164
        $this->setData($manialink, $listData);
165
    }
166
167
    /**
168
     * @param ManialinkInterface $manialink
169
     * @param $login
170
     * @param $entries
171
     * @param $args
172
     */
173
    public function callbackRemovePlayer(
174
        $manialink,
175
        $login,
176
        $entries,
177
        $args
178
    ) {
179
        try {
180
            $this->executeForPlayer($args['login']);
181
            $this->updateData($manialink);
182
        } catch (\Exception $e) {
183
            $this->chatNotification->sendMessage('expansion_players.chat.exception',
184
                $login, ["%message%" => $e->getMessage()]);
185
        }
186
        $this->update($manialink->getUserGroup());
187
    }
188
189
    /**
190
     * @param $login
191
     * @param $command
192
     */
193
    public function callChatCommand($login, $command)
194
    {
195
        $this->chatCommandDataProvider->onPlayerChat($login, $login, $command, true);
196
    }
197
198
}
199
200