Completed
Pull Request — master (#144)
by
unknown
02:41
created

JukeboxWindowFactory::updateMaps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 12
cp 0
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 0
crap 6
1
<?php
2
3
4
namespace eXpansion\Bundle\Maps\Plugins\Gui;
5
6
use eXpansion\Bundle\Maps\Plugins\Jukebox;
7
use eXpansion\Bundle\Maps\Services\JukeboxService;
8
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
9
use eXpansion\Framework\Core\Helpers\Time;
10
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
11
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
12
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
13
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
14
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
15
use eXpansion\Framework\Gui\Components\uiButton;
16
use Maniaplanet\DedicatedServer\Structures\Map;
17
18
19
/**
20
 * Class RecordsWindowFactory
21
 *
22
 * @package eXpansion\Bundle\LocalRecords\Plugins\Gui;
23
 * @author  reaby
24
 */
25
class JukeboxWindowFactory extends GridWindowFactory
26
{
27
    public $sizeX;
28
    public $sizeY;
29
30
    /** @var GridBuilderFactory */
31
    protected $gridBuilderFactory;
32
33
    /** @var DataCollectionFactory */
34
    protected $dataCollectionFactory;
35
36
    /** @var Time */
37
    protected $timeFormatter;
38
    /**
39
     * @var Jukebox
40
     */
41
    private $jukeboxPlugin;
42
    /**
43
     * @var JukeboxService
44
     */
45
    private $jukeboxService;
46
    /**
47
     * @var AdminGroups
48
     */
49
    private $adminGroups;
50
51
    /**
52
     * MapsWindowFactory constructor.
53
     * @param $name
54
     * @param $sizeX
55
     * @param $sizeY
56
     * @param null $posX
57
     * @param null $posY
58
     * @param WindowFactoryContext $context
59
     * @param GridBuilderFactory $gridBuilderFactory
60
     * @param DataCollectionFactory $dataCollectionFactory
61
     * @param Time $time
62
     * @param JukeboxService $jukeboxService
63
     * @param AdminGroups $adminGroups
64
     */
65 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...
66
        $name,
67
        $sizeX,
68
        $sizeY,
69
        $posX,
70
        $posY,
71
        WindowFactoryContext $context,
72
        GridBuilderFactory $gridBuilderFactory,
73
        DataCollectionFactory $dataCollectionFactory,
74
        Time $time,
75
        JukeboxService $jukeboxService,
76
        AdminGroups $adminGroups
77
    ) {
78
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
79
80
        $this->gridBuilderFactory = $gridBuilderFactory;
81
        $this->dataCollectionFactory = $dataCollectionFactory;
82
        $this->timeFormatter = $time;
83
        $this->sizeX = $sizeX;
84
        $this->sizeY = $sizeY;
85
        $this->jukeboxService = $jukeboxService;
86
        $this->adminGroups = $adminGroups;
87
    }
88
89
    public function setJukeboxPlugin(Jukebox $plugin)
90
    {
91
        $this->jukeboxPlugin = $plugin;
92
    }
93
94
    /**
95
     * @param ManialinkInterface $manialink
96
     * @return void
97
     */
98
    protected function createGrid(ManialinkInterface $manialink)
99
    {
100
        $this->setData($manialink, $this->updateMaps());
101
102
        $queueButton = $this->uiFactory->createButton('drop', uiButton::TYPE_DEFAULT);
103
        $queueButton->setTextColor("000")->setSize(12, 5)->setTranslate(true);
104
105
        $gridBuilder = $this->gridBuilderFactory->create();
106
        $gridBuilder->setManialink($manialink)
107
            ->setDataCollection($manialink->getData('dataCollection'))
108
            ->setManialinkFactory($this)
109
            ->addTextColumn(
110
                'index',
111
                'expansion_maps.gui.window.column.index',
112
                1,
113
                true,
114
                false
115
            )->addTextColumn(
116
                'name',
117
                'expansion_maps.gui.window.column.name',
118
                3,
119
                true,
120
                false
121
            )->addTextColumn(
122
                'time',
123
                'expansion_maps.gui.window.column.goldtime',
124
                2,
125
                true,
126
                false
127
            )->addTextColumn(
128
                'nickname',
129
                'expansion_maps.gui.window.column.nickname',
130
                3,
131
                true,
132
                false
133
            );
134
135
136
        if ($this->adminGroups->hasPermission($manialink->getUserGroup(), "admin")) {
137
            $gridBuilder->addActionColumn('map', 'expansion_maps.gui.window.column.drop', 2,
138
                array($this, 'callbackDropMap'),
139
                $queueButton);
140
        }
141
        $contentFrame = $manialink->getContentFrame();
142
        $this->setGridSize($contentFrame->getWidth(), $contentFrame->getHeight() - 12);
143
        $this->setGridPosition(0, -10);
144
        $manialink->setData('grid', $gridBuilder);
145
146
    }
147
148 View Code Duplication
    public function callbackClear(ManialinkInterface $manialink, $login, $entries, $args)
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...
149
    {
150
        $this->jukeboxPlugin->clear($login);
151
        $group = $this->groupFactory->createForPlayer($login);
152
        $this->setData($manialink, $this->updateMaps());
153
        $this->update($group);
154
    }
155
156 View Code Duplication
    public function callbackDrop(ManialinkInterface $manialink, $login, $entries, $args)
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...
157
    {
158
159
        $this->jukeboxPlugin->drop($login, null);
160
        $group = $this->groupFactory->createForPlayer($login);
161
        $this->setData($manialink, $this->updateMaps());
162
        $this->update($group);
163
164
    }
165
166 View Code Duplication
    public function callbackDropMap(ManialinkInterface $manialink, $login, $params, $args)
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...
167
    {
168
        $this->jukeboxPlugin->drop($login, $args['map']);
169
        $group = $this->groupFactory->createForPlayer($login);
170
        $this->setData($manialink, $this->updateMaps());
171
        $this->update($group);
172
    }
173
174
    public function createContent(ManialinkInterface $manialink)
175
    {
176
        parent::createContent($manialink);
177
        $line = $this->uiFactory->createLayoutLine(0, 0, [], 2);
178
179
        $dropButton = $this->uiFactory->createButton("expansion_maps.gui.button.drop", uiButton::TYPE_DECORATED);
180
        $dropButton->setTranslate(true);
181
        $dropButton->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackDrop'], null));
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
182
        $line->addChild($dropButton);
183
184
        $clearButton = $this->uiFactory->createButton("expansion_maps.gui.button.clear");
185
        $clearButton->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackClear'],
186
            null))
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
187
            ->setFocusColor('f00')
188
            ->setBorderColor('d00')
189
            ->setTextColor('fff')
190
            ->setTranslate(true);
191
192
        if ($this->adminGroups->hasPermission($manialink->getUserGroup(), "jukebox")) {
193
            $line->addChild($clearButton);
194
        }
195
        $manialink->addChild($line);
196
    }
197
198
    /**
199
     * @return array
200
     */
201
    public function updateMaps()
202
    {
203
        /**
204
         * @var string $i
205
         * @var Map $map
206
         */
207
        $i = 1;
208
        $data = [];
209
        foreach ($this->jukeboxService->getMapQueue() as $idx => $jbMap) {
210
            $map = $jbMap->getMap();
211
            $data[] = [
212
                'index' => $i++,
213
                'name' => $map->name,
214
                'nickname' => $jbMap->getPlayer()->getNickName(),
215
                'time' => $this->timeFormatter->timeToText($map->goldTime, true),
216
                'map' => $jbMap,
217
            ];
218
        }
219
220
        return $data;
221
    }
222
223
224
}
225