Completed
Pull Request — master (#86)
by De Cramer
02:25
created

WindowHelpFactory::setChatCommandDataProvide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
    /**
38
     * @param GridBuilderFactory $gridBuilderFactory
39
     */
40
    public function setGridBuilderFactory($gridBuilderFactory)
41
    {
42
        $this->gridBuilderFactory = $gridBuilderFactory;
43
    }
44
45
    /**
46
     * @param DataCollectionFactory $dataCollectionFactory
47
     */
48
    public function setDataCollectionFactory($dataCollectionFactory)
49
    {
50
        $this->dataCollectionFactory = $dataCollectionFactory;
51
    }
52
53
    /**
54
     * @param ChatCommands $chatCommands
55
     */
56
    public function setChatCommands($chatCommands)
57
    {
58
        $this->chatCommands = $chatCommands;
59
    }
60
61
    public function setChatCommandDataProvide($chatCommandDataPovider)
62
    {
63
        $this->chatCommandDataPovider = $chatCommandDataPovider;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    protected function createContent(ManialinkInterface $manialink)
70
    {
71
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
72
        $collection->setPageSize(2);
73
74
        $helpButton = new Label();
75
        $helpButton->setText('')
76
            ->setSize(6, 6)
77
            ->setAreaColor("0000")
78
            ->setAreaFocusColor("0000");
79
80
        $gridBuilder = $this->gridBuilderFactory->create();
81
        $gridBuilder->setManialink($manialink)
82
            ->setDataCollection($collection)
83
            ->setManialinkFactory($this)
84
            ->addTextColumn(
85
                'command',
86
                "expansion_core.windows.chat_commands.column_command",
87
                25
88
            )
89
            ->addTextColumn(
90
                'description',
91
                'expansion_core.windows.chat_commands.column_description',
92
                70,
93
                false,
94
                true
95
            )
96
            ->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...
97
98
        $manialink->setData('grid', $gridBuilder);
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 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...
105
    {
106
        /** @var Frame $contentFrame */
107
        $contentFrame = $manialink->getContentFrame();
108
        $contentFrame->removeAllChildren();
109
110
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
111
        $collection->setPageSize(20);
112
113
        /** @var GridBuilder $gridBuilder */
114
        $gridBuilder = $manialink->getData('grid');
115
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
116
    }
117
118
    /**
119
     * Get chat commands to display the admin.
120
     *
121
     * @param ManialinkInterface $manialink
122
     *
123
     * @return array
124
     */
125
    protected function getChatCommands(ManialinkInterface $manialink)
126
    {
127
        $login = $manialink->getUserGroup()->getLogins()[0];
128
129
        return array_map(
130
            function($command) {
131
                /** @var AbstractChatCommand $command */
132
                return [
133
                    'command' => $command->getCommand(),
134
                    'description' => $command->getDescription(),
135
                    'help' => $command->getHelp(),
136
                    'aliases' => $command->getAliases(),
137
                ];
138
            },
139
            array_filter(
140
                $this->chatCommands->getChatCommands(),
141
                function ($command) use ($login) {
142
                    if ($command instanceof AbstractAdminChatCommand) {
143
                        return $command->hasPermission($login);
144
                    }
145
                    return true;
146
                }
147
148
            )
149
        );
150
    }
151
152
    /**
153
     * Callbacked called when help button is pressed.
154
     *
155
     * @param $login
156
     * @param $params
157
     * @param $arguments
158
     */
159
    public function callbackHelp($login, $params, $arguments)
160
    {
161
        $this->chatCommandDataPovider->onPlayerChat(0, $login, '/'.$arguments['command'].' -h', true);
162
    }
163
}
164