Completed
Push — master ( 285bc9...932fc1 )
by Marko
14s
created

CoreController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 96
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A dashboardAction() 0 6 1
A searchAction() 0 6 1
A getRequest() 0 10 1
A getAdminPool() 0 4 1
A getSearchHandler() 0 4 1
A getBaseTemplate() 0 8 2
A getTemplateRegistry() 0 4 1
A getCurrentRequest() 0 4 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 37 and the first side effect is on line 18.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Controller;
15
16
// NEXT_MAJOR: remove this file
17
18
@trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
19
    'The '.__NAMESPACE__.'\CoreController class is deprecated since version 3.x and will be removed in 4.0.'
20
    .' Use '.__NAMESPACE__.'\SearchAction or '.__NAMESPACE__.'\DashboardAction instead.',
21
    E_USER_DEPRECATED
22
);
23
24
use Sonata\AdminBundle\Action\DashboardAction;
25
use Sonata\AdminBundle\Action\SearchAction;
26
use Sonata\AdminBundle\Admin\Pool;
27
use Sonata\AdminBundle\Search\SearchHandler;
28
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
29
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
30
use Symfony\Component\HttpFoundation\JsonResponse;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\Response;
33
34
/**
35
 * @author Thomas Rabaix <[email protected]>
36
 */
37
class CoreController extends Controller
38
{
39
    /**
40
     * @return Response
41
     */
42
    public function dashboardAction()
43
    {
44
        $dashboardAction = $this->container->get(DashboardAction::class);
45
46
        return $dashboardAction($this->getCurrentRequest());
47
    }
48
49
    /**
50
     * The search action first render an empty page, if the query is set, then the template generates
51
     * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
52
     *
53
     * @throws \RuntimeException
54
     *
55
     * @return JsonResponse|Response
56
     */
57
    public function searchAction(Request $request)
58
    {
59
        $searchAction = $this->container->get(SearchAction::class);
60
61
        return $searchAction($request);
62
    }
63
64
    /**
65
     * Get the request object from the container.
66
     *
67
     * This method is compatible with both Symfony 2.3 and Symfony 3
68
     *
69
     * NEXT_MAJOR: remove this method.
70
     *
71
     * @deprecated since 3.0, to be removed in 4.0 and action methods will be adjusted.
72
     *             Use Symfony\Component\HttpFoundation\Request as an action argument
73
     *
74
     * @return Request
75
     */
76
    public function getRequest()
77
    {
78
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
            'The '.__METHOD__.' method is deprecated since 3.0 and will be removed in 4.0.'.
80
            ' Inject the Symfony\Component\HttpFoundation\Request into the actions instead.',
81
            E_USER_DEPRECATED
82
        );
83
84
        return $this->getCurrentRequest();
85
    }
86
87
    /**
88
     * @return Pool
89
     */
90
    protected function getAdminPool()
91
    {
92
        return $this->container->get('sonata.admin.pool');
93
    }
94
95
    /**
96
     * @return SearchHandler
97
     */
98
    protected function getSearchHandler()
99
    {
100
        return $this->get('sonata.admin.search.handler');
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    protected function getBaseTemplate()
107
    {
108
        if ($this->getCurrentRequest()->isXmlHttpRequest()) {
109
            return $this->getTemplateRegistry()->getTemplate('ajax');
110
        }
111
112
        return $this->getTemplateRegistry()->getTemplate('layout');
113
    }
114
115
    /**
116
     * @return TemplateRegistryInterface
117
     */
118
    private function getTemplateRegistry()
119
    {
120
        return $this->container->get('sonata.admin.global_template_registry');
121
    }
122
123
    /**
124
     * Get the request object from the container.
125
     *
126
     * @return Request
127
     */
128
    private function getCurrentRequest()
129
    {
130
        return $this->container->get('request_stack')->getCurrentRequest();
131
    }
132
}
133