Completed
Push — master ( 265a2d...f9e310 )
by jerome
02:58
created

GameServerController   B

Complexity

Total Complexity 22

Size/Duplication

Total Lines 164
Duplicated Lines 23.17 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 16
dl 38
loc 164
rs 8.4614

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 14 14 2
A installProgressAction() 0 15 4
A regenAction() 0 9 1
B logsAction() 24 24 2
B rconAction() 0 42 6
B pluginAction() 0 37 6
A createRconForm() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\GameServerBundle\Controller;
13
14
use DP\Core\CoreBundle\Controller\Server\ServerController;
15
use DP\GameServer\GameServerBundle\Entity\GameServer;
16
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
17
use DP\Core\CoreBundle\Exception\NotImplementedException;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
22
23
class GameServerController extends ServerController
24
{
25
    /**
26
     * @var GameServerDomainManager
27
     */
28
    protected $domainManager;
29
30 View Code Duplication
    public function setContainer(ContainerInterface $container = null)
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...
31
    {
32
        parent::setContainer($container);
33
34
        if ($container !== null) {
35
            $this->domainManager = new GameServerDomainManager(
36
                $container->get($this->config->getServiceName('manager')),
37
                $container->get('event_dispatcher'),
38
                $this->flashHelper,
39
                $this->config,
40
                $container->get('twig')
41
            );
42
        }
43
    }
44
45
    public function installProgressAction(Request $request)
46
    {
47
        if (!$this->isGranted('CREATE', $this->find($request)) && !$this->isGranted('UPDATE', $this->find($request))) {
48
            throw new AccessDeniedException;
49
        }
50
51
        /** @var GameServer $server */
52
        $server = $this->findOr404($request);
53
54
        if (!$server->isInstallationEnded()) {
55
            $this->domainManager->getInstallationProgress($server);
56
        }
57
        
58
        return $this->redirectHandler->redirectToReferer();
59
    }
60
    
61
    public function regenAction(Request $request)
62
    {
63
        $this->isGrantedOr403('ADMIN', $this->find($request));
64
        
65
        $server = $this->findOr404($request);
66
        $this->domainManager->regenerateConfig($server);
67
68
        return $this->redirectHandler->redirectToReferer();
69
    }
70
    
71 View Code Duplication
    public function logsAction(Request $request)
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...
72
    {
73
        $this->isGrantedOr403('ADMIN', $this->find($request));
74
        
75
        $config = $this->getConfiguration();   
76
        $server = $this->findOr404($request);
77
78
        $logs = $this->domainManager->getServerLogs($server);
79
80
        if ($logs === null) {
81
            return $this->redirectHandler->redirectToReferer();
82
        }
83
        
84
        $view = $this
85
            ->view()
86
            ->setTemplate($config->getTemplate('logs.html'))
87
            ->setData(array(
88
                $config->getResourceName() => $server,
89
                'logs'                     => $logs
90
            ))
91
        ;
92
93
        return $this->handleView($view);
94
    }
95
96
    public function rconAction(Request $request)
97
    {
98
        $this->isGrantedOr403('RCON', $this->find($request));
99
        
100
        $config = $this->getConfiguration();
101
        $server = $this->findOr404($request);
102
        $form = $this->createRconForm();
103
        
104
        $logs = $server->getServerLogs() . "\n";
105
106
        if (!$this->domainManager->isAccessibleFromQuery($server)) {
107
            return $this->redirectHandler->redirectToReferer();
108
        }
109
110
        if ($request->isMethod('POST') && $form->submit($request)->isValid()) {
111
            $data = $form->getData();
112
            $cmd = $data['cmd'];
113
114
            $ret = $this->domainManager->executeRconCmd($server, $cmd);
115
            $logs .= '> ' . $cmd . "\n" . $ret . "\n";
116
117
            if ($config->isApiRequest()) {
118
                return $this->handleView($this->view(array('log' => $logs, 'cmd' => $cmd, 'ret' => $ret)));
119
            }
120
        }
121
        
122
        if ($config->isApiRequest()) {
123
            return $this->handleView($this->view(array('form' => $form, 'log' => $logs)));
124
        }
125
126
        $view = $this
127
            ->view()
128
            ->setTemplate($config->getTemplate('rcon.html'))
129
            ->setData(array(
130
                $config->getResourceName() => $server,
131
                'form'                     => $form->createView(), 
132
                'log'                      => $logs,
133
            ))
134
        ;
135
136
        return $this->handleView($view);
137
    }
138
    
139
    public function pluginAction(Request $request)
140
    {
141
        $this->isGrantedOr403('PLUGIN', $this->find($request));
142
        
143
        $config = $this->getConfiguration();
144
        $server = $this->findOr404($request);
145
146
        $pluginId = $request->get('plugin');
147
        $action   = $request->get('action');
148
        
149
        if ($pluginId && $action) {
150
            $em = $this->getDoctrine()->getManager();
151
            $plugin = $em->getRepository('DPGameBundle:Plugin')->findOneBy(array('id' => $pluginId));
152
153
            if (!$plugin) {
154
                throw new NotFoundHttpException('Requested plugin does not exist.');
155
            }
156
            if ($action != 'install' && $action != 'uninstall') {
157
                throw new NotImplementedException();
158
            }
159
160
            $method = $action . 'Plugin';
161
            $server = $this->domainManager->{$method}($server, $plugin);
0 ignored issues
show
Unused Code introduced by
$server is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
163
            return $this->redirectHandler->redirectToReferer();
164
        }
165
        
166
        $view = $this
167
            ->view()
168
            ->setTemplate($config->getTemplate('plugin.html'))
169
            ->setData(array(
170
                $config->getResourceName() => $server,
171
            ))
172
        ;
173
174
        return $this->handleView($view);
175
    }
176
177
    public function createRconForm(array $default = array())
178
    {
179
        $form = $this
180
            ->createFormBuilder($default)
181
            ->add('cmd', 'text', array('label' => 'game.rcon.command'))
182
        ;
183
184
        return $form->getForm();
185
    }
186
}
187