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

WindowHelpDetailsFactory::updateContent()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 114
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 8.2857
c 0
b 0
f 0
ccs 0
cts 92
cp 0
cc 2
eloc 62
nc 2
nop 1
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace eXpansion\Framework\Core\Plugins\Gui;
5
use eXpansion\Framework\Core\Helpers\Translations;
6
use eXpansion\Framework\Core\Model\ChatCommand\AbstractChatCommand;
7
use eXpansion\Framework\Core\Model\Gui\Factory\LineFactory;
8
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
9
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
10
11
/**
12
 * Class WindowHelpDetailsFactory
13
 *
14
 * @package eXpansion\Framework\Core\Plugins\Gui;
15
 * @author  oliver de Cramer <[email protected]>
16
 */
17
class WindowHelpDetailsFactory extends WindowFactory
18
{
19
    /** @var AbstractChatCommand */
20
    protected $currentCommand = null;
21
22
    /** @var LineFactory */
23
    protected $lineFactory;
24
25
    /** @var LineFactory */
26
    protected $titleLineFactory;
27
28
    public function __construct(
29
        $name,
30
        $sizeX,
31
        $sizeY,
32
        $posX,
33
        $posY,
34
        WindowFactoryContext $context,
35
        LineFactory $lineFactory,
36
        LineFactory $titleLineFactory
37
    ) {
38
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
39
40
        $this->lineFactory = $lineFactory;
41
        $this->titleLineFactory = $titleLineFactory;
42
    }
43
44
45
    /**
46
     * @inheritdoc
47
     */
48
    protected function createContent(ManialinkInterface $manialink)
49
    {
50
        $manialink->setData('command', $this->currentCommand);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function updateContent(ManialinkInterface $manialink)
57
    {
58
        $manialink->getContentFrame()->removeAllChildren();
59
60
        /** @var AbstractChatCommand $command */
61
        $command = $manialink->getData('command');
62
63
        $column1Width = $manialink->getContentFrame()->getWidth() * (2/3);
64
        $column2Width = $manialink->getContentFrame()->getWidth() * (1/3) - 1;
65
66
        /*
67
         * COLUMN 1 Description of the chat command
68
         */
69
        $descriptionTitle = $this->titleLineFactory->create(
70
            20,
71
            [
72
                [
73
                    'text' => 'expansion_core.windows.chat_commands_description.description',
74
                    'width' => 1,
75
                    'translatable' => true
76
                ]
77
            ],
78
            0
79
        );
80
        $manialink->addChild($descriptionTitle);
81
82
        $descriptionLabel = $this->lineFactory->create(
83
            $column1Width - 21,
84
            [
85
                [
86
                    'text' => $command->getDescription(),
87
                    'width' => 1,
88
                    'translatable' => true
89
90
                ]
91
            ],
92
            0,
93
            16,
94
            true,
95
            4
96
        );
97
        $descriptionLabel->setPosition(21, 0);
98
        $manialink->addChild($descriptionLabel);
99
100
        $helpTitle = $this->titleLineFactory->create(
101
            20,
102
            [
103
                [
104
                    'text' => 'expansion_core.windows.chat_commands_description.help',
105
                    'width' => 1,
106
                    'translatable' => true
107
                ]
108
            ],
109
            0
110
        );
111
        $helpTitle->setPosition(0, -1 * ($descriptionLabel->getHeight() + 1));
112
        $manialink->addChild($helpTitle);
113
114
        $helpLabel = $this->lineFactory->create(
115
            $column1Width - 21,
116
            [
117
                [
118
                    'text' => "Use '/" . $this->currentCommand->getCommand() . " -h' to get help on the usage of the command.",
119
                    'width' => 1,
120
                    'translatable' => false
121
122
                ]
123
            ],
124
            0,
125
            8,
126
            true,
127
            2
128
        );
129
        $helpLabel->setPosition(21, $helpTitle->getY());
130
        $manialink->addChild($helpLabel);
131
132
        /**
133
         * COLUMN2 Aliases of the chat command.
134
         */
135
        $aliasesTitle = $this->titleLineFactory->create(
136
            20,
137
            [
138
                [
139
                    'text' => 'expansion_core.windows.chat_commands_description.aliases',
140
                    'width' => 1,
141
                    'translatable' => true
142
                ]
143
            ],
144
            0
145
        );
146
        $aliasesTitle->setPosition($column1Width + 1, 0);
147
        $manialink->addChild($aliasesTitle);
148
149
        $posY = 0;
150
        $idx = 0;
151
        foreach ($command->getAliases() as $i => $alias) {
152
            $aliasesLabel = $this->lineFactory->create(
153
                $column2Width - 21,
154
                [
155
                    [
156
                        'text' => "/$alias",
157
                        'width' => 1,
158
                        'translatable' => false
159
160
                    ]
161
                ],
162
                $idx++
163
            );
164
            $aliasesLabel->setPosition($column1Width + 22, $posY);
165
            $manialink->addChild($aliasesLabel);
166
167
            $posY -= ($aliasesLabel->getHeight() + 1);
168
        }
169
    }
170
171
    /**
172
     * @param null $currentCommand
173
     */
174
    public function setCurrentCommand($currentCommand)
175
    {
176
        $this->currentCommand = $currentCommand;
177
    }
178
}
179