Completed
Push — master ( e7ee3b...814f23 )
by De Cramer
17s
created

WindowHelpFactory::callbackDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
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\Services\ChatCommands;
13
use FML\Controls\Frame;
14
use FML\Controls\Label;
15
16
17
/**
18
 * Class HelpFactory
19
 *
20
 * @package eXpansion\Framework\AdminGroups\Plugins\Window;
21
 * @author  oliver de Cramer <[email protected]>
22
 */
23
class WindowHelpFactory extends WindowFactory
24
{
25
    /** @var GridBuilderFactory */
26
    protected $gridBuilderFactory;
27
28
    /** @var DataCollectionFactory */
29
    protected $dataCollectionFactory;
30
31
    /** @var ChatCommands */
32
    protected $chatCommands;
33
34
    /** @var ChatCommandDataProvider */
35
    protected $chatCommandDataPovider;
36
37
    /** @var WindowHelpDetailsFactory */
38
    protected $windowHelpDetailsFactory;
39
40
    /**
41
     * @param GridBuilderFactory $gridBuilderFactory
42
     */
43
    public function setGridBuilderFactory($gridBuilderFactory)
44
    {
45
        $this->gridBuilderFactory = $gridBuilderFactory;
46
    }
47
48
    /**
49
     * @param DataCollectionFactory $dataCollectionFactory
50
     */
51
    public function setDataCollectionFactory($dataCollectionFactory)
52
    {
53
        $this->dataCollectionFactory = $dataCollectionFactory;
54
    }
55
56
    /**
57
     * @param ChatCommands $chatCommands
58
     */
59
    public function setChatCommands($chatCommands)
60
    {
61
        $this->chatCommands = $chatCommands;
62
    }
63
64
    /**
65
     * @param ChatCommandDataProvider $chatCommandDataPovider
66
     */
67
    public function setChatCommandDataProvide($chatCommandDataPovider)
68
    {
69
        $this->chatCommandDataPovider = $chatCommandDataPovider;
70
    }
71
72
    /**
73
     * @param WindowHelpDetailsFactory $windowHelpDetailsFactory
74
     */
75
    public function setWindowDescriptionFactory(WindowHelpDetailsFactory $windowHelpDetailsFactory)
76
    {
77
        $this->windowHelpDetailsFactory = $windowHelpDetailsFactory;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    protected function createContent(ManialinkInterface $manialink)
84
    {
85
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
86
        $collection->setPageSize(2);
87
88
        $helpButton = new Label();
89
        $helpButton->setText('')
90
            ->setSize(6, 6)
91
            ->setAreaColor("0000")
92
            ->setAreaFocusColor("0000");
93
94
        $desctiptionButton = new Label();
95
        $desctiptionButton->setText('')
96
            ->setSize(6, 6)
97
            ->setAreaColor("0000")
98
            ->setAreaFocusColor("0000");
99
100
        $gridBuilder = $this->gridBuilderFactory->create();
101
        $gridBuilder->setManialink($manialink)
102
            ->setDataCollection($collection)
103
            ->setManialinkFactory($this)
104
            ->addTextColumn(
105
                'command',
106
                "expansion_core.windows.chat_commands.column_command",
107
                25
108
            )
109
            ->addTextColumn(
110
                'description',
111
                'expansion_core.windows.chat_commands.column_description',
112
                70,
113
                false,
114
                true
115
            )
116
            ->addActionColumn('help', '', 5, array($this, 'callbackHelp'), $helpButton)
0 ignored issues
show
Documentation introduced by
$helpButton is of type object<FML\Controls\Label>, but the function expects a object<eXpansion\Framewo...e\Model\Gui\Grid\Label>.

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...
117
            ->addActionColumn(
118
                'description',
119
                '',
120
                5,
121
                array($this, 'callbackDescription'),
122
                $desctiptionButton
0 ignored issues
show
Documentation introduced by
$desctiptionButton is of type object<FML\Controls\Label>, but the function expects a object<eXpansion\Framewo...e\Model\Gui\Grid\Label>.

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...
123
            );
124
125
        $manialink->setData('grid', $gridBuilder);
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131 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...
132
    {
133
        /** @var Frame $contentFrame */
134
        $contentFrame = $manialink->getContentFrame();
135
        $contentFrame->removeAllChildren();
136
137
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
138
        $collection->setPageSize(20);
139
140
        /** @var GridBuilder $gridBuilder */
141
        $gridBuilder = $manialink->getData('grid');
142
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
143
    }
144
145
    /**
146
     * Get chat commands to display the admin.
147
     *
148
     * @param ManialinkInterface $manialink
149
     *
150
     * @return array
151
     */
152
    protected function getChatCommands(ManialinkInterface $manialink)
153
    {
154
        $login = $manialink->getUserGroup()->getLogins()[0];
155
156
        return array_map(
157
            function($command) {
158
                /** @var AbstractChatCommand $command */
159
                return [
160
                    'command' => $command->getCommand(),
161
                    'description' => $command->getDescription(),
162
                    'help' => $command->getHelp(),
163
                    'aliases' => $command->getAliases(),
164
                ];
165
            },
166
            array_filter(
167
                $this->chatCommands->getChatCommands(),
168
                function ($command) use ($login) {
169
                    if ($command instanceof AbstractAdminChatCommand) {
170
                        return $command->hasPermission($login);
171
                    }
172
                    return true;
173
                }
174
175
            )
176
        );
177
    }
178
179
    /**
180
     * Callbacked called when help button is pressed.
181
     *
182
     * @param $login
183
     * @param $params
184
     * @param $arguments
185
     */
186
    public function callbackHelp($login, $params, $arguments)
187
    {
188
        $this->chatCommandDataPovider->onPlayerChat(0, $login, '/'.$arguments['command'].' -h', true);
189
    }
190
191
    /**
192
     * Callbacked called when description button is pressed.
193
     *
194
     * @param $login
195
     * @param $params
196
     * @param $arguments
197
     */
198
    public function callbackDescription($login, $params, $arguments)
199
    {
200
        $chatCommands = $this->chatCommands->getChatCommands();
201
        $this->windowHelpDetailsFactory->setCurrentCommand($chatCommands[$arguments['command']]);
202
203
        $this->windowHelpDetailsFactory->create($login);
204
    }
205
}
206