Completed
Push — master ( 5bc326...973a57 )
by Benjamin
05:33 queued 02:50
created

AdminCommandController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B commandAction() 0 33 6
A listAction() 0 21 3
A commandFlushCache() 0 10 1
A executeCommand() 0 21 3
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Controller;
4
5
use Sonata\AdminBundle\Controller\CRUDController as Controller;
6
use Symfony\Bundle\FrameworkBundle\Console\Application;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * @author Alexis BUSSIERES <[email protected]>
14
 */
15
class AdminCommandController extends Controller
16
{
17
    public function commandAction(): Response
18
    {
19
        $request     = $this->getRequest();
20
        $query       = $request->query;
21
        $command     = $query->get('name');
22
        $environment = $this->get('kernel')->getEnvironment();
23
24
        if ($query->has('flush_cache') && $query->getInt('flush_cache') === 1) {
25
            $responseFlush = $this->commandFlushCache();
26
            if ($environment === 'dev') {
27
                if ($responseFlush->isSuccessful()) {
28
                    $this->addFlash('success', 'Les caches ont bien été vidés');
29
                } else {
30
                    $this->addFlash('error', 'Une erreur est survenue lors de la supression des caches !');
31
                }
32
            }
33
        }
34
35
        $responseCommand = $this->executeCommand(['command' => $command]);
36
        if ($responseCommand->isSuccessful()) {
37
            $this->addFlash(
38
                'success',
39
                sprintf('La commande "%s" a bien été exécuté.', $command)
40
            );
41
        } else {
42
            $this->addFlash(
43
                'error',
44
                sprintf('Une erreur est survenue lors de l\'exécution de la commande "%s"!', $command)
45
            );
46
        }
47
48
        return $this->redirectTo($this->admin->generateUrl('list'));
0 ignored issues
show
Documentation introduced by
$this->admin->generateUrl('list') is of type string, but the function expects a object.

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...
49
    }
50
51
    public function listAction()
52
    {
53
        $request = $this->getRequest();
54
55
        $this->admin->checkAccess('list');
56
57
        $preResponse = $this->preList($request);
58
        if ($preResponse !== null) {
59
            return $preResponse;
60
        }
61
62
        if ($listMode = $request->get('_list_mode')) {
63
            $this->admin->setListMode($listMode);
64
        }
65
66
        return $this->render('AlpixelCMSBundle:admin/page:base_list.html.twig', array(
67
            'object'     => null,
68
            'action'     => 'list',
69
            'csrf_token' => $this->getCsrfToken('sonata.batch'),
70
        ), null, $request);
0 ignored issues
show
Unused Code introduced by
The call to AdminCommandController::render() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
    }
72
73
    protected function commandFlushCache(): Response
74
    {
75
        $environment = $this->get('kernel')->getEnvironment();
76
        $inputs      = [
77
            'command' => 'cache:clear',
78
            '--env'   => $environment,
79
        ];
80
81
        return $this->executeCommand($inputs);
82
    }
83
84
    protected function executeCommand(array $inputs): Response
85
    {
86
        if (empty($inputs) || !isset($inputs['command'])) {
87
            throw new \Exception('The array is empty or "command" index is missing');
88
        }
89
90
        $kernel      = $this->get('kernel');
91
        $application = new Application($kernel);
92
        $application->setAutoExit(false);
93
94
        $input  = new ArrayInput($inputs);
95
        $output = new BufferedOutput(
96
            OutputInterface::VERBOSITY_NORMAL,
97
            true
98
        );
99
100
        $application->run($input, $output);
101
        $content = $output->fetch();
102
103
        return new Response($content);
104
    }
105
}
106