Completed
Pull Request — master (#99)
by De Cramer
02:42
created

WindowHelpFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 11
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\Framework\Core\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Model\AbstractAdminChatCommand;
6
use eXpansion\Framework\Core\DataProviders\ChatCommandDataProvider;
7
use eXpansion\Framework\Core\Model\ChatCommand\AbstractChatCommand;
8
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
9
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
10
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
13
use eXpansion\Framework\Core\Services\ChatCommands;
14
use FML\Controls\Frame;
15
use FML\Controls\Label;
16
17
18
/**
19
 * Class HelpFactory
20
 *
21
 * @package eXpansion\Framework\AdminGroups\Plugins\Window;
22
 * @author  oliver de Cramer <[email protected]>
23
 */
24
class WindowHelpFactory extends WindowFactory
25
{
26
    /** @var GridBuilderFactory */
27
    protected $gridBuilderFactory;
28
29
    /** @var DataCollectionFactory */
30
    protected $dataCollectionFactory;
31
32
    /** @var ChatCommands */
33
    protected $chatCommands;
34
35
    /** @var ChatCommandDataProvider */
36
    protected $chatCommandDataPovider;
37
38
    /** @var WindowHelpDetailsFactory */
39
    protected $windowHelpDetailsFactory;
40
41
    public function __construct(
42
        $name,
43
        $sizeX,
44
        $sizeY,
45
        $posX,
46
        $posY,
47
        WindowFactoryContext $context,
48
        GridBuilderFactory $gridBuilderFactory,
49
        DataCollectionFactory $dataCollectionFactory,
50
        ChatCommands $chatCommands,
51
        ChatCommandDataProvider $chatCommandDataProvider,
52
        WindowHelpDetailsFactory $windowHelpDetailsFactory
53
    ) {
54
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
55
56
        $this->gridBuilderFactory = $gridBuilderFactory;
57
        $this->dataCollectionFactory = $dataCollectionFactory;
58
        $this->chatCommands = $chatCommands;
59
        $this->chatCommandDataPovider = $chatCommandDataProvider;
60
        $this->windowHelpDetailsFactory = $windowHelpDetailsFactory;
61
    }
62
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function createContent(ManialinkInterface $manialink)
68
    {
69
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
70
        $collection->setPageSize(2);
71
72
        $helpButton = new Label();
73
        $helpButton->setText('')
74
            ->setSize(6, 6)
75
            ->setAreaColor("0000")
76
            ->setAreaFocusColor("0000");
77
78
        $desctiptionButton = new Label();
79
        $desctiptionButton->setText('')
80
            ->setSize(6, 6)
81
            ->setAreaColor("0000")
82
            ->setAreaFocusColor("0000");
83
84
        $gridBuilder = $this->gridBuilderFactory->create();
85
        $gridBuilder->setManialink($manialink)
86
            ->setDataCollection($collection)
87
            ->setManialinkFactory($this)
88
            ->addTextColumn(
89
                'command',
90
                "expansion_core.windows.chat_commands.column_command",
91
                25
92
            )
93
            ->addTextColumn(
94
                'description',
95
                'expansion_core.windows.chat_commands.column_description',
96
                70,
97
                false,
98
                true
99
            )
100
            ->addActionColumn('help', '', 5, array($this, 'callbackHelp'), $helpButton)
101
            ->addActionColumn(
102
                'description',
103
                '',
104
                5,
105
                array($this, 'callbackDescription'),
106
                $desctiptionButton
107
            );
108
109
        $manialink->setData('grid', $gridBuilder);
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 View Code Duplication
    protected function updateContent(ManialinkInterface $manialink)
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...
116
    {
117
        /** @var Frame $contentFrame */
118
        $contentFrame = $manialink->getContentFrame();
119
        $contentFrame->removeAllChildren();
120
121
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
122
        $collection->setPageSize(20);
123
124
        /** @var GridBuilder $gridBuilder */
125
        $gridBuilder = $manialink->getData('grid');
126
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
127
    }
128
129
    /**
130
     * Get chat commands to display the admin.
131
     *
132
     * @param ManialinkInterface $manialink
133
     *
134
     * @return array
135
     */
136
    protected function getChatCommands(ManialinkInterface $manialink)
137
    {
138
        $login = $manialink->getUserGroup()->getLogins()[0];
139
140
        return array_map(
141
            function($command) {
142
                /** @var AbstractChatCommand $command */
143
                return [
144
                    'command' => $command->getCommand(),
145
                    'description' => $command->getDescription(),
146
                    'help' => $command->getHelp(),
147
                    'aliases' => $command->getAliases(),
148
                ];
149
            },
150
            array_filter(
151
                $this->chatCommands->getChatCommands(),
152
                function ($command) use ($login) {
153
                    if ($command instanceof AbstractAdminChatCommand) {
154
                        return $command->hasPermission($login);
155
                    }
156
                    return true;
157
                }
158
159
            )
160
        );
161
    }
162
163
    /**
164
     * Callbacked called when help button is pressed.
165
     *
166
     * @param $login
167
     * @param $params
168
     * @param $arguments
169
     */
170
    public function callbackHelp($login, $params, $arguments)
171
    {
172
        $this->chatCommandDataPovider->onPlayerChat(0, $login, '/'.$arguments['command'].' -h', true);
173
    }
174
175
    /**
176
     * Callbacked called when description button is pressed.
177
     *
178
     * @param $login
179
     * @param $params
180
     * @param $arguments
181
     */
182
    public function callbackDescription($login, $params, $arguments)
183
    {
184
        $chatCommands = $this->chatCommands->getChatCommands();
185
        $this->windowHelpDetailsFactory->setCurrentCommand($chatCommands[$arguments['command']]);
186
187
        $this->windowHelpDetailsFactory->create($login);
188
    }
189
}
190