Completed
Push — master ( 57cff2...8bf774 )
by De Cramer
10s
created

WindowHelpFactory::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 46
ccs 0
cts 38
cp 0
rs 8.9411
cc 1
eloc 37
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\Model\ChatCommand\AbstractChatCommand;
7
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
9
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
12
use eXpansion\Framework\Core\Services\ChatCommands;
13
use eXpansion\Framework\GameManiaplanet\DataProviders\ChatCommandDataProvider;
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 $chatCommandDataProvider;
37
38
    /** @var WindowHelpDetailsFactory */
39
    protected $windowHelpDetailsFactory;
40
41 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...
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->chatCommandDataProvider = $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(4, 4)
75
            ->setAreaColor("0000")
76
            ->setAreaFocusColor("000a");
77
78
        $descriptionButton = new Label();
79
        $descriptionButton->setText('')
80
            ->setSize(4, 4)
81
            ->setAreaColor("0000")
82
            ->setAreaFocusColor("000a");
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
                true,
93
                false
94
            )
95
            ->addTextColumn(
96
                'description',
97
                'expansion_core.windows.chat_commands.column_description',
98
                70,
99
                false,
100
                true
101
            )
102
            ->addActionColumn('help', '', 5, [$this, 'callbackCallCommand'], $helpButton)
103
            ->addActionColumn(
104
                'description',
105
                '',
106
                5,
107
                [$this, 'callbackDescription'],
108
                $descriptionButton
109
            );
110
111
        $manialink->setData('grid', $gridBuilder);
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 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...
118
    {
119
        /** @var Frame $contentFrame */
120
        $contentFrame = $manialink->getContentFrame();
121
        $contentFrame->removeAllChildren();
122
123
        $collection = $this->dataCollectionFactory->create($this->getChatCommands($manialink));
124
        $collection->setPageSize(20);
125
126
        /** @var GridBuilder $gridBuilder */
127
        $gridBuilder = $manialink->getData('grid');
128
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
129
    }
130
131
    /**
132
     * Get chat commands to display the admin.
133
     *
134
     * @param ManialinkInterface $manialink
135
     *
136
     * @return array
137
     */
138
    protected function getChatCommands(ManialinkInterface $manialink)
139
    {
140
        $login = $manialink->getUserGroup()->getLogins()[0];
141
142
        return array_map(
143
            function ($command) {
144
                /** @var AbstractChatCommand $command */
145
                return [
146
                    'command' => $command->getCommand(),
147
                    'description' => $command->getDescription(),
148
                    'help' => $command->getHelp(),
149
                    'aliases' => $command->getAliases(),
150
                ];
151
            },
152
            array_filter(
153
                $this->chatCommands->getChatCommands(),
154
                function ($command) use ($login) {
155
                    if ($command instanceof AbstractAdminChatCommand) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Admi...bstractAdminChatCommand does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
156
                        return $command->hasPermission($login);
157
                    }
158
159
                    return true;
160
                }
161
162
            )
163
        );
164
    }
165
166
    /**
167
     * Callback called when help button is pressed.
168
     *
169
     * @param ManialinkInterface $manialink
170
     * @param                    $login
171
     * @param                    $params
172
     * @param                    $arguments
173
     */
174
    public function callbackCallCommand(ManialinkInterface $manialink, $login, $params, $arguments)
175
    {
176
        $this->chatCommandDataProvider->onPlayerChat(-1, $login, '/'.$arguments['command'], true);
177
    }
178
179
    /**
180
     * Callbacked called when description button is pressed.
181
     *
182
     * @param ManialinkInterface $manialink
183
     * @param                    $login
184
     * @param                    $params
185
     * @param                    $arguments
186
     */
187
    public function callbackDescription(ManialinkInterface $manialink, $login, $params, $arguments)
188
    {
189
        $chatCommands = $this->chatCommands->getChatCommands();
190
        $this->windowHelpDetailsFactory->setCurrentCommand($chatCommands[$arguments['command']]);
191
192
        $this->windowHelpDetailsFactory->create($login);
193
    }
194
}
195