ConfigModifier::preUpdate()   D
last analyzed

Complexity

Conditions 23
Paths 22

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
rs 4.7895
cc 23
eloc 27
nc 22
nop 1

How to fix   Complexity   

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
 * 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\GameServer\MinecraftServerBundle\Listener;
13
14
use Doctrine\ORM\Event\PreUpdateEventArgs;
15
use DP\GameServer\MinecraftServerBundle\Entity\MinecraftServer;
16
use DP\Core\MachineBundle\Entity\Machine;
17
18
/**
19
 * @author Albin Kerouanton 
20
 */
21
class ConfigModifier
22
{
23
    protected $container;
24
    
25
    /**
26
     * Set service container
27
     * 
28
     * @param ServiceContainer $container
29
     */
30
    public function setServiceContainer($container)
31
    {
32
        $this->container = $container;
33
    }
34
    
35
    /**
36
     * Get twig
37
     * 
38
     * @throws Exception 
39
     * @return \Twig_Environment
40
     */
41
    protected function getTwig()
42
    {
43
        if (is_null($this->container)) {
44
            throw new Exception('The service container is not yet set.');
45
        }
46
        
47
        return $this->container->get('twig');
48
    }
49
    
50
    /**
51
     * Maj des scripts du serveur si la config du serveur (port, maxplayers, dir) a été modifié
52
     * Ou si la config du jeu (bin, binDir, launchName, map, orangebox, source) a été modifié
53
     * Ou si l'IP publique de la machine a été modifié
54
     * 
55
     * Réinstallation du serveur
56
     * Si des modifs ont été faites sur la machine (IP publique, home, user)
57
     * Ou si le serveur de jeu n'est plus sur la meme machine
58
     * Ou si le jeu du serveur est modifié
59
     * 
60
     * @param \Doctrine\ORM\Event\PreUpdateEventArgs $args
61
     */
62
    public function preUpdate(PreUpdateEventArgs $args)
63
    {
64
        $entity = $args->getEntity();
65
        
66
        if ($entity instanceof MinecraftServer) {
67
            if ($args->hasChangedField('port') || $args->hasChangedField('maxplayers') 
68
                || $args->hasChangedField('name') || $args->hasChangedField('queryPort') 
69
                || $args->hasChangedField('rconPort') || $args->hasChangedField('rconPassword')) {
70
                try {
71
                    $entity->modifyServerPropertiesFile();
72
                }
73
                catch (\Exception $e) {}
74
            }
75
            if ($args->hasChangedField('minHeap') || $args->hasChangedField('maxHeap')
76
                || $args->hasChangedField('dir') || $args->hasChangedField('core')) {
77
                try {
78
                    $entity->uploadShellScripts($this->getTwig());
79
                }
80
                catch (\Exception $e) {}
81
            }
82
        }
83
        elseif ($entity instanceof Machine) {
84
            // Upload des scripts si l'IP public ou le home de la machine a été modifié
85
            if ($args->hasChangedField('publicIp') || $args->hasChangedField('home')) {
86
                $servers = $entity->getGameServers();
87
                
88
                foreach ($servers AS $server) {
89
                    if (!$server instanceof MinecraftServer) continue;
90
                    
91
                    if ($args->hasChangedField('publicIp')) {
92
                        try {
93
                            $server->modifyServerPropertiesFile();
94
                        }
95
                        catch (\Exception $e) {}
96
                    }
97
                    if ($args->hasChangedField('home')) {
98
                        try  {
99
                            $server->uploadShellScripts($this->getTwig());
100
                        }
101
                        catch (\Exception $e) {}
102
                    }
103
                }
104
            }
105
        }
106
    }
107
}
108