Completed
Push — master ( 82efd5...5f5ed3 )
by
unknown
14s
created

JukeboxWindowFactory::createGrid()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 39
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 38
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace eXpansion\Bundle\Maps\Plugins\Gui;
5
6
use eXpansion\Bundle\Acme\Plugins\Gui\WindowFactory;
7
use eXpansion\Bundle\LocalRecords\Entity\Record;
8
use eXpansion\Bundle\Maps\Plugins\Jukebox;
9
use eXpansion\Bundle\Maps\Services\JukeboxService;
10
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
11
use eXpansion\Framework\Core\Helpers\Time;
12
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
13
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
14
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
15
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
16
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
17
use eXpansion\Framework\Core\Model\UserGroups\Group;
18
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
19
use eXpansion\Framework\Gui\Components\uiButton;
20
use FML\Controls\Frame;
21
use Maniaplanet\DedicatedServer\Structures\Map;
22
23
24
/**
25
 * Class RecordsWindowFactory
26
 *
27
 * @package eXpansion\Bundle\LocalRecords\Plugins\Gui;
28
 * @author  reaby
29
 */
30
class JukeboxWindowFactory extends GridWindowFactory
31
{
32
    public $sizeX;
33
    public $sizeY;
34
35
    /** @var GridBuilderFactory */
36
    protected $gridBuilderFactory;
37
38
    /** @var DataCollectionFactory */
39
    protected $dataCollectionFactory;
40
41
    /** @var Time */
42
    protected $timeFormatter;
43
    /**
44
     * @var Jukebox
45
     */
46
    private $jukeboxPlugin;
47
    /**
48
     * @var JukeboxService
49
     */
50
    private $jukeboxService;
51
    /**
52
     * @var AdminGroups
53
     */
54
    private $adminGroups;
55
56
    /**
57
     * MapsWindowFactory constructor.
58
     * @param $name
59
     * @param $sizeX
60
     * @param $sizeY
61
     * @param null $posX
62
     * @param null $posY
63
     * @param WindowFactoryContext $context
64
     * @param GridBuilderFactory $gridBuilderFactory
65
     * @param DataCollectionFactory $dataCollectionFactory
66
     * @param Time $time
67
     * @param JukeboxService $jukeboxService
68
     * @param AdminGroups $adminGroups
69
     */
70 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...
71
        $name,
72
        $sizeX,
73
        $sizeY,
74
        $posX,
75
        $posY,
76
        WindowFactoryContext $context,
77
        GridBuilderFactory $gridBuilderFactory,
78
        DataCollectionFactory $dataCollectionFactory,
79
        Time $time,
80
        JukeboxService $jukeboxService,
81
        AdminGroups $adminGroups
82
    ) {
83
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
84
85
        $this->gridBuilderFactory = $gridBuilderFactory;
86
        $this->dataCollectionFactory = $dataCollectionFactory;
87
        $this->timeFormatter = $time;
88
        $this->sizeX = $sizeX;
89
        $this->sizeY = $sizeY;
90
        $this->jukeboxService = $jukeboxService;
91
        $this->adminGroups = $adminGroups;
92
    }
93
94
    public function setJukeboxPlugin(Jukebox $plugin)
95
    {
96
        $this->jukeboxPlugin = $plugin;
97
    }
98
99
    /**
100
     * @param ManialinkInterface $manialink
101
     * @return void
102
     */
103
    protected function createGrid(ManialinkInterface $manialink)
104
    {
105
        $this->setData($manialink, $this->updateMaps());
106
107
        $queueButton = $this->uiFactory->createButton('drop', uiButton::TYPE_DEFAULT);
108
        $queueButton->setTextColor("000")->setSize(12, 5)->setTranslate(true);
109
110
        $gridBuilder = $this->gridBuilderFactory->create();
111
        $gridBuilder->setManialink($manialink)
112
            ->setDataCollection($manialink->getData('dataCollection'))
113
            ->setManialinkFactory($this)
114
            ->addTextColumn(
115
                'index',
116
                'expansion_maps.gui.window.column.index',
117
                1,
118
                true,
119
                false
120
            )->addTextColumn(
121
                'name',
122
                'expansion_maps.gui.window.column.name',
123
                3,
124
                true,
125
                false
126
            )->addTextColumn(
127
                'time',
128
                'expansion_maps.gui.window.column.goldtime',
129
                2,
130
                true,
131
                false
132
            )->addTextColumn(
133
                'nickname',
134
                'expansion_maps.gui.window.column.nickname',
135
                3,
136
                true,
137
                false
138
            )
139
            ->addActionColumn('map', 'expansion_maps.gui.window.column.drop', 2, array($this, 'callbackDropMap'),
140
                $queueButton);
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 View Code Duplication
    public function callbackClear(ManialinkInterface $manialink, $login, $entries, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $entries is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
148
    {
149
        $this->jukeboxPlugin->clear($login);
150
        $group = $this->groupFactory->createForPlayer($login);
151
        $this->setData($manialink, $this->updateMaps());
152
        $this->update($group);
153
    }
154
155 View Code Duplication
    public function callbackDrop(ManialinkInterface $manialink, $login, $entries, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $entries is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
156
    {
157
158
        $this->jukeboxPlugin->drop($login, null);
159
        $group = $this->groupFactory->createForPlayer($login);
160
        $this->setData($manialink, $this->updateMaps());
161
        $this->update($group);
162
163
    }
164
165 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...
166
    {
167
        $this->jukeboxPlugin->drop($login, $args['map']);
168
        $group = $this->groupFactory->createForPlayer($login);
169
        $this->setData($manialink, $this->updateMaps());
170
        $this->update($group);
171
    }
172
173
    public function createContent(ManialinkInterface $manialink)
174
    {
175
        $line = $this->uiFactory->createLayoutLine(0, 0, [], 2);
176
177
        $dropButton = $this->uiFactory->createButton("expansion_maps.gui.button.drop", uiButton::TYPE_DECORATED);
178
        $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...
179
        $line->addChild($dropButton);
180
181
        $clearButton = $this->uiFactory->createButton("expansion_maps.gui.button.clear");
182
        $clearButton->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackClear'],
183
            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...
184
            ->setFocusColor('f00')
185
            ->setBorderColor('d00')
186
            ->setTextColor('fff')
187
            ->setTranslate(true);
188
189
        if ($this->adminGroups->hasPermission($manialink->getUserGroup(), "jukebox")) {
190
            $line->addChild($clearButton);
191
        }
192
        $manialink->addChild($line);
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function updateMaps()
199
    {
200
        /**
201
         * @var string $i
202
         * @var Map $map
203
         */
204
        $i = 1;
205
        $data = [];
206
        foreach ($this->jukeboxService->getMapQueue() as $idx => $jbMap) {
207
            $map = $jbMap->getMap();
208
            $data[] = [
209
                'index' => $i++,
210
                'name' => $map->name,
211
                'nickname' => $jbMap->getPlayer()->getNickName(),
212
                'time' => $this->timeFormatter->timeToText($map->goldTime, true),
213
                'map' => $jbMap,
214
            ];
215
        }
216
217
        return $data;
218
    }
219
220
221
}
222