AdminCommandController::commandAction()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.7697
c 0
b 0
f 0
cc 6
nc 8
nop 0
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()
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', [
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Contr...RUDController::render() has been deprecated with message: since version 3.27, to be removed in 4.0. Use Sonata\AdminBundle\Controller\CRUDController::renderWithExtraParams() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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()
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)
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