Game   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 527
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 4

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 45
c 8
b 0
f 0
lcom 5
cbo 4
dl 0
loc 527
rs 8.3673

39 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getId() 0 4 1
A setName() 0 4 1
A getName() 0 4 1
A setLaunchName() 0 4 1
A getLaunchName() 0 4 1
A setBin() 0 4 1
A getBin() 0 4 1
A setMap() 0 4 1
A getMap() 0 4 1
A setAvailable() 0 4 1
A getAvailable() 0 4 1
A __toString() 0 4 1
A setBinDir() 0 4 1
A getBinDir() 0 8 2
A setCfgPath() 0 4 1
A getCfgPath() 0 4 1
A setSourceImagesMaps() 0 4 1
A getSourceImagesMaps() 0 4 1
A addPlugin() 0 8 2
A removePlugin() 0 4 1
A setPlugins() 0 4 1
A getPlugins() 0 4 1
A setType() 0 4 1
A getType() 0 4 1
A isBukkit() 0 4 1
A setConfigTemplate() 0 4 1
A getConfigTemplate() 0 4 1
A setAppId() 0 6 1
A getAppId() 0 4 1
A setAppMod() 0 6 1
A getAppMod() 0 4 1
A setSource() 0 4 1
A isSource() 0 4 1
A getSource() 0 4 1
A addGameServer() 0 6 1
A removeGameServer() 0 4 1
A getGameServers() 0 4 1
B validateAppId() 0 14 5

How to fix   Complexity   

Complex Class

Complex classes like Game often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Game, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DP\Core\GameBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Knp\DictionaryBundle\Validator\Constraints\Dictionary;
16
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
17
use Symfony\Component\Validator\Constraints as Assert;
18
use Symfony\Component\Validator\Context\ExecutionContextInterface;
19
20
/**
21
 * DP\Core\GameBundle\Entity\Game
22
 * @author Albin Kerouanton
23
 *
24
 * @ORM\Table(name="game")
25
 * @ORM\Entity(repositoryClass="DP\Core\GameBundle\Entity\GameRepository")
26
 * @Assert\Callback(methods={"validateAppId"})
27
 * @UniqueEntity(fields="name", message="game.assert.name.unique")
28
 * @UniqueEntity(fields={"appId","appMod"}, message="game.assert.unique_id_mod")
29
 */
30
class Game
31
{
32
    /**
33
     * @var integer $id
34
     *
35
     * @ORM\Column(name="id", type="integer")
36
     * @ORM\Id
37
     * @ORM\GeneratedValue(strategy="AUTO")
38
     */
39
    private $id;
40
41
    /**
42
     * @var string $name
43
     *
44
     * @ORM\Column(name="name", type="string", length=32)
45
     * @Assert\NotBlank(message="game.assert.name.needed")
46
     */
47
    private $name;
48
49
    /**
50
     * @var boolean $steamCmd
51
     *
52
     * @ORM\Column(name="steamCmd", type="boolean")
53
     */
54
    private $steamCmd = false; // default value
55
56
    /**
57
     * @var boolean $source
58
     *
59
     * @ORM\Column(name="source", type="boolean")
60
     */
61
    private $source = false;
62
63
    /**
64
     * @var string $launchName
65
     *
66
     * @ORM\Column(name="launchName", type="string", length=24)
67
     * @Assert\NotBlank(message="game.assert.launchName")
68
     */
69
    private $launchName;
70
71
    /**
72
     * @var string $bin
73
     *
74
     * @ORM\Column(name="bin", type="string", length=24)
75
     * @Assert\NotBlank(message="game.assert.bin")
76
     */
77
    private $bin;
78
79
    /**
80
     * @var integer $appId
81
     *
82
     * @ORM\Column(name="appId", type="integer", nullable=true)
83
     */
84
    protected $appId;
85
86
    /**
87
     * @var string $appMod
88
     *
89
     * @ORM\Column(name="appMod", type="string", length=20, nullable=true)
90
     */
91
    protected $appMod;
92
93
    /**
94
     * @var string $map
95
     *
96
     * @ORM\Column(name="map", type="string", length=40, nullable=true)
97
     */
98
    private $map;
99
100
    /**
101
     * @var boolean $available
102
     *
103
     * @ORM\Column(name="available", type="boolean")
104
     * @Assert\NotNull(message="game.assert.available")
105
     */
106
    private $available = true;
107
108
    /**
109
     * @var string $binDir
110
     *
111
     * @ORM\Column(name="binDir", type="string", length=20, nullable=true)
112
     */
113
    private $binDir;
114
115
   /**
116
     * @var string $cfgPath
117
     *
118
     * @ORM\Column(name="cfgPath", type="string", length=255, nullable=true)
119
     */
120
    protected $cfgPath;
121
122
    /**
123
     * @var string $cfgPath
124
     *
125
     * @ORM\Column(name="cfgPath", type="string", length=255, nullable=true)
126
     */
127
    protected $cfgPath;
128
129
    /**
130
     * @ORM\Column(name="sourceImagesMaps", type="string", length=255, nullable=true)
131
     * @var string
132
     */
133
    private $sourceImagesMaps;
134
135
    /**
136
     * @var \Doctrine\Common\Collections\ArrayCollection $gameServers
137
     *
138
     * @ORM\OneToMany(targetEntity="DP\GameServer\GameServerBundle\Entity\GameServer", mappedBy="game", cascade={"remove"})
139
     */
140
    private $gameServers;
141
142
    /**
143
     * @var \Doctrine\Common\Collections\ArrayCollection $plugins
144
     *
145
     * @ORM\ManyToMany(targetEntity="DP\Core\GameBundle\Entity\Plugin", inversedBy="games")
146
     * @ORM\JoinTable(name="game_plugin",
147
     *      joinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")},
148
     *      inverseJoinColumns={@ORM\JoinColumn(name="plugin_id", referencedColumnName="id")}
149
     * )
150
     */
151
    private $plugins;
152
153
    /**
154
     * @ORM\Column(name="type", type="string", length=32)
155
     * @Dictionary(name="game_type", message="game.assert.type")
156
     */
157
    private $type;
158
159
    /**
160
     * @ORM\Column(name="configTemplate", type="text", nullable=true)
161
     */
162
    private $configTemplate;
163
164
165
    public function __construct()
166
    {
167
        $this->plugins = new \Doctrine\Common\Collections\ArrayCollection(array());
168
    }
169
170
    /**
171
     * Get id
172
     *
173
     * @return integer
174
     */
175
    public function getId()
176
    {
177
        return $this->id;
178
    }
179
180
    /**
181
     * Set name
182
     *
183
     * @param string $name
184
     */
185
    public function setName($name)
186
    {
187
        $this->name = $name;
188
    }
189
190
    /**
191
     * Get name
192
     *
193
     * @return string
194
     */
195
    public function getName()
196
    {
197
        return $this->name;
198
    }
199
200
    /**
201
     * Set launchName
202
     *
203
     * @param string $launchName
204
     */
205
    public function setLaunchName($launchName)
206
    {
207
        $this->launchName = $launchName;
208
    }
209
210
    /**
211
     * Get launchName
212
     *
213
     * @return string
214
     */
215
    public function getLaunchName()
216
    {
217
        return $this->launchName;
218
    }
219
220
    /**
221
     * Set bin
222
     *
223
     * @param string $bin
224
     */
225
    public function setBin($bin)
226
    {
227
        $this->bin = $bin;
228
    }
229
230
    /**
231
     * Get bin
232
     *
233
     * @return string
234
     */
235
    public function getBin()
236
    {
237
        return $this->bin;
238
    }
239
240
    /**
241
     * Set map
242
     *
243
     * @param string $map
244
     */
245
    public function setMap($map)
246
    {
247
        $this->map = $map;
248
    }
249
250
    /**
251
     * Get map
252
     *
253
     * @return string
254
     */
255
    public function getMap()
256
    {
257
        return $this->map;
258
    }
259
260
    /**
261
     * Set available
262
     *
263
     * @param boolean $available
264
     */
265
    public function setAvailable($available)
266
    {
267
        $this->available = $available;
268
    }
269
270
    /**
271
     * Get available
272
     *
273
     * @return boolean
274
     */
275
    public function getAvailable()
276
    {
277
        return $this->available;
278
    }
279
280
    public function __toString()
281
    {
282
        return $this->name;
283
    }
284
285
    /**
286
     * Set binary directory
287
     *
288
     * @param string $binDir
289
     */
290
    public function setBinDir($binDir)
291
    {
292
        $this->binDir = $binDir;
293
    }
294
295
    /**
296
     * Get binary directory
297
     *
298
     * @return string
299
     */
300
    public function getBinDir()
301
    {
302
        if (empty($this->binDir)) {
303
            return '';
304
        }
305
        
306
        return $this->binDir;
307
    }
308
309
    /**
310
     * Set cfg path
311
     *
312
     * @param string $cfgPath
313
     */
314
    public function setCfgPath($cfgPath)
315
    {
316
        $this->cfgPath = $cfgPath;
317
    }
318
319
    /**
320
321
     * Get cfg path
322
     *
323
     * @return string
324
     */
325
    public function getCfgPath()
326
    {
327
        return $this->cfgPath;
328
    }
329
330
331
    /**
332
     * Set source of images maps
333
     *
334
     * @param string $sourceImagesMaps
335
     */
336
    public function setSourceImagesMaps($sourceImagesMaps)
337
    {
338
        $this->sourceImagesMaps = $sourceImagesMaps;
339
    }
340
341
    /**
342
     * Get source of images maps
343
     *
344
     * @return string
345
     */
346
    public function getSourceImagesMaps()
347
    {
348
        return $this->sourceImagesMaps;
349
    }
350
351
    /**
352
     * Add plugins
353
     *
354
     * @param  $plugin \Doctrine\Common\Collections\ArrayCollection
355
     */
356
    public function addPlugin(\DP\Core\GameBundle\Entity\Plugin $plugin)
357
    {
358
        $this->plugins[] = $plugin;
359
360
        if (!$plugin->getGames()->contains($this)) {
361
            $plugin->addGame($this);
362
        }
363
    }
364
365
    public function removePlugin(Plugin $plugin)
366
    {
367
        $this->plugins->removeElement($plugin);
368
    }
369
370
    /**
371
     * Set plugin list
372
     *
373
     * @param array $plugins
374
     */
375
    public function setPlugins(array $plugins = array())
376
    {
377
        $this->plugins = new \Doctrine\Common\Collections\ArrayCollection($plugins);
378
    }
379
380
    /**
381
     * Get plugins
382
     *
383
     * @return \Doctrine\Common\Collections\ArrayCollection
384
     */
385
    public function getPlugins()
386
    {
387
        return $this->plugins;
388
    }
389
390
    /**
391
     * Set game type (steam or minecraft)
392
     *
393
     * @param string $type
394
     */
395
    public function setType($type)
396
    {
397
        $this->type = $type;
398
    }
399
400
    /**
401
     * @return string Game type
402
     */
403
    public function getType()
404
    {
405
        return $this->type;
406
    }
407
408
    public function isBukkit()
409
    {
410
        return $this->getLaunchName() == 'bukkit';
411
    }
412
413
    /**
414
     * Set the server config file template
415
     * @param string|null $configTemplate
416
     */
417
    public function setConfigTemplate($configTemplate)
418
    {
419
        $this->configTemplate = $configTemplate;
420
    }
421
422
    /**
423
     * Get the server config file template
424
     *
425
     * @return string
426
     */
427
    public function getConfigTemplate()
428
    {
429
        return $this->configTemplate;
430
    }
431
432
    /**
433
     * Set appId
434
     *
435
     * @param integer $appId
436
     * @return Game
437
     */
438
    public function setAppId($appId)
439
    {
440
        $this->appId = $appId;
441
442
        return $this;
443
    }
444
445
    /**
446
     * Get appId
447
     *
448
     * @return integer
449
     */
450
    public function getAppId()
451
    {
452
        return $this->appId;
453
    }
454
455
    /**
456
     * Set appMod
457
     *
458
     * @param string $appMod
459
     * @return Game
460
     */
461
    public function setAppMod($appMod)
462
    {
463
        $this->appMod = $appMod;
464
465
        return $this;
466
    }
467
468
    /**
469
     * Get appMod
470
     *
471
     * @return integer
472
     */
473
    public function getAppMod()
474
    {
475
        return $this->appMod;
476
    }
477
478
    /**
479
     * Set source
480
     *
481
     * @param boolean $source
482
     */
483
    public function setSource($source)
484
    {
485
        $this->source = $source;
486
    }
487
488
    /**
489
     * Get source
490
     *
491
     * @return boolean
492
     */
493
    public function isSource()
494
    {
495
        return $this->source;
496
    }
497
498
499
    /**
500
     * Get source
501
     *
502
     * @return boolean
503
     */
504
    public function getSource()
505
    {
506
        return $this->source;
507
    }
508
509
    /**
510
     * Add gameServers
511
     *
512
     * @param \DP\GameServer\GameServerBundle\Entity\GameServer $gameServers
513
     * @return Game
514
     */
515
    public function addGameServer(\DP\GameServer\GameServerBundle\Entity\GameServer $gameServers)
516
    {
517
        $this->gameServers[] = $gameServers;
518
519
        return $this;
520
    }
521
522
    /**
523
     * Remove gameServers
524
     *
525
     * @param \DP\GameServer\GameServerBundle\Entity\GameServer $gameServers
526
     */
527
    public function removeGameServer(\DP\GameServer\GameServerBundle\Entity\GameServer $gameServers)
528
    {
529
        $this->gameServers->removeElement($gameServers);
530
    }
531
532
    /**
533
     * Get gameServers
534
     *
535
     * @return \Doctrine\Common\Collections\Collection
536
     */
537
    public function getGameServers()
538
    {
539
        return $this->gameServers;
540
    }
541
542
    public function validateAppId(ExecutionContextInterface $context)
543
    {
544
        $appId = $this->getAppId();
545
        
546
        if ('steam' === $this->getType() && empty($appId)) {
547
            $context->buildViolation('game.assert.appId.needed')
548
                ->atPath('appId')
549
                ->addViolation();
550
        } elseif ('steam' !== $this->getType() && !empty($appId)) {
551
            $context->buildViolation('game.assert.appId.not_needed')
552
                ->atPath('appId')
553
                ->addViolation();
554
        }
555
    }
556
}
557