Completed
Push — master ( fdbeef...265a2d )
by jerome
25:01 queued 21:35
created

FTPController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\ResourceController;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use DP\GameServer\GameServerBundle\Entity\GameServer;
20
use Dedipanel\PHPSeclibWrapperBundle\SFTP\AbstractItem;
21
use Dedipanel\PHPSeclibWrapperBundle\SFTP\Directory;
22
use Dedipanel\PHPSeclibWrapperBundle\SFTP\File;
23
use Dedipanel\PHPSeclibWrapperBundle\SFTP\Exception\InvalidPathException;
24
25
/**
26
 * @todo: Apply criteria & sorting
27
 * @todo: refacto phpseclib
28
 */
29
class FTPController extends ResourceController
30
{
31
    const TYPE_FILE = 'file';
32
    const TYPE_DIRECTORY  = 'directory';
33
34
    /**
35
     * @var FTPDomainManager
36
     */
37
    protected $domainManager;
38
39
40
    public function setContainer(ContainerInterface $container = null)
41
    {
42
        parent::setContainer($container);
43
44
        if ($container !== null) {
45
            $this->domainManager = new FTPDomainManager(
46
                $container->get($this->config->getServiceName('manager')),
47
                $container->get('event_dispatcher'),
48
                $this->flashHelper,
49
                $this->config
50
            );
51
        }
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     * 
57
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
58
     */
59
    public function indexAction(Request $request)
60
    {
61
        throw new NotFoundHttpException();
62
    }
63
    
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function showAction(Request $request)
68
    {
69
        $this->isGrantedOr403('FTP', $this->find($request));
70
        
71
        $config = $this->getConfiguration();
72
        /** @var GameServer $server */
73
        $server = $this->findOr404($request);
74
75
        $resource = $this->getResource($server, $request->get('path'));
76
77
        if ($resource instanceof File) {
78
            throw new MethodNotAllowedHttpException(array('POST', 'PUT'));
79
        }
80
        
81
        $view = $this
82
            ->view()
83
            ->setTemplate($config->getTemplate('show.html'))
84
            ->setData(array(
85
                'server' => $server,
86
                'resource' => $resource,
87
            ))
88
        ;
89
90
        return $this->handleView($view);
91
    }
92
    
93
    /**
94
     * {@inheritdoc}
95
     */
96 View Code Duplication
    public function createAction(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...
97
    {
98
        $this->isGrantedOr403('CREATE');
99
        
100
        $config = $this->getConfiguration();
101
        $server = $this->findOr404($request);
102
103
        $resource = $this->createResource($server,
104
            $request->get('path'),
105
            $request->get('type')
106
        );
107
        $form     = $this->getForm($resource);
108
        
109
        $form->handleRequest($request);
110
111
        if ($form->isSubmitted()) {
112
            if ($form->isValid()) {
113
                $resource = $this->domainManager->createResource($server, $resource);
114
115
                return $this->redirectTo($server, $resource);
116
            }
117
        }
118
        
119
        if ($config->isApiRequest()) {
120
            return $this->handleView($this->view($form));
121
        }
122
        
123
        $view = $this
124
            ->view()
125
            ->setTemplate($config->getTemplate('create.html'))
126
            ->setData(array(
127
                'server'   => $server,
128
                'form'     => $form->createView(),
129
                'resource' => $resource,
130
            ))
131
        ;
132
        
133
        return $this->handleView($view);
134
    }
135
136
    /**
137
     * Display the form for editing or update the resource.
138
     */
139 View Code Duplication
    public function updateAction(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...
140
    {
141
        $this->isGrantedOr403('UPDATE', $this->find($request));
142
143
        $config = $this->getConfiguration();
144
        $server = $this->findOr404($request);
145
        
146
        $resource = $this->getResource($server, $request->get('path'));
147
        $form     = $this->getForm($resource);
148
        
149
        $form->handleRequest($request);
150
151
        if ($form->isSubmitted()) {
152
            if ($form->isValid()) {
153
                $resource = $this->domainManager->updateResource($server, $resource);
154
155
                return $this->redirectTo($server, $resource);
156
            }
157
        }
158
159
        if ($config->isApiRequest()) {
160
            return $this->handleView($this->view($form));
161
        }
162
163
        $view = $this
164
            ->view()
165
            ->setTemplate($config->getTemplate('update.html'))
166
            ->setData(array(
167
                'server'   => $server,
168
                'form'     => $form->createView(),
169
                'resource' => $resource,
170
            ))
171
        ;
172
173
        return $this->handleView($view);
174
    }
175
176
    /**
177
     * Delete resource.
178
     */
179
    public function deleteAction(Request $request)
180
    {
181
        $this->isGrantedOr403('DELETE', $this->find($request));
182
        
183
        $server   = $this->findOr404($request);
184
        $resource = $this->getResource($server, $request->get('path'));
185
        
186
        if ($request->request->get('confirmed', false)) {
187
            $resource = $this->domainManager->deleteResource($server, $resource);
188
189
            if ($request->isXmlHttpRequest()) {
190
                return JsonResponse::create(array('id' => $request->get('id')));
191
            }
192
193
            $this->flashHelper->setFlash('success', 'delete');
194
195
            $config = $this->getConfiguration();
196
            $parameters = $config->getRedirectParameters();
197
198
            if (empty($parameters)) {
199
                $parameters['id'] = $server->getId();
200
                $parameters['path'] = $resource->getPath();
201
            }
202
203
            return $this->redirectHandler->redirectToRoute(
204
                $config->getRedirectRoute('show'),
205
                $parameters
206
            );
207
        }
208
209
        if ($request->isXmlHttpRequest()) {
210
            throw new AccessDeniedHttpException;
211
        }
212
213
        $view = $this
214
            ->view()
215
            ->setTemplate($request->attributes->get('template', 'SyliusWebBundle:Backend/Misc:delete.html.twig'))
216
            ->setData(array(
217
                'server'   => $server,
218
                'resource' => $resource,
219
            ))
220
        ;
221
222
        return $this->handleView($view);
223
    }
224
225
    /**
226
     * Get a new resource
227
     *
228
     * @param GameServer $server
229
     * @param string $path
230
     * @param string $type
231
     * @return Directory|File
232
     * @throws \RuntimeException
233
     */
234
    public function createResource(GameServer $server, $path, $type)
235
    {
236
        if ($type != self::TYPE_FILE && $type != self::TYPE_DIRECTORY) {
237
            throw new \RuntimeException('Not supported ftp resource type.');
238
        }
239
240
        $conn    = $server->getMachine()->getConnection();
241
        $gameDir = $server->getAbsoluteGameContentDir();
242
        $path    = '~/' . $path;
243
244
        if ($type == self::TYPE_FILE) {
245
            $item = new File($conn, $path, $gameDir, true);
246
        }
247
        else {
248
            $item = new Directory($conn, $path, $gameDir, true);
249
        }
250
251
        return $item;
252
    }
253
254
    /**
255
     * @param GameServer $server
256
     * @param string $path
257
     * @return Dedipanel\PHPSeclibWrapperBundle\SFTP\AbstractItem
258
     */
259
    public function getResource(GameServer $server, $path)
260
    {
261
        $path = '~/' . $path;
262
263
        /** @var Dedipanel\PHPSeclibWrapperBundle\SFTP\SFTPItemFactory $factory */
264
        $factory  = $this->get('dedipanel.sftp_factory');
265
266
        try {
267
            $resource = $factory->getItem(
268
                $server->getMachine()->getConnection(),
269
                $path,
270
                $server->getAbsoluteGameContentDir()
271
            );
272
        }
273
        catch (InvalidPathException $e) {
274
            throw new NotFoundHttpException($e->getMessage());
275
        }
276
277
        return $resource;
278
    }
279
    
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function getForm($resource = null)
284
    {
285
        if ($resource instanceof File) {
286
            $formType = 'dedipanel_game_ftp_file';
287
        }
288
        elseif ($resource instanceof Directory) {
289
            $formType = 'dedipanel_game_ftp_directory';
290
        }
291
        else {
292
            throw new \RuntimeException('Not supported ftp resource type.');
293
        }
294
        
295
        return $this->createForm($formType, $resource);
296
    }
297
    
298
    /**
299
     * {@inheritdoc}
300
     */
301
    public function redirectTo(GameServer $server, AbstractItem $resource)
302
    {
303
        $config = $this->getConfiguration();
304
305
        $parameters = $config->getRedirectParameters();
306
        $route = $config->getRedirectRoute('show');
307
        
308
        if (empty($parameters)) {
309
            $parameters['id'] = $server->getId();
310
            $parameters['path'] = $resource->getRelativePath();
311
312
            if ($resource instanceof File) {
313
                $parameters['path'] = $resource->getPath();
314
            }
315
        }
316
317
        return $this->redirectHandler->redirectToRoute($route, $parameters);
318
    }
319
}
320