Completed
Pull Request — master (#6194)
by Vincent
05:04
created

CoreController::getTemplateRegistry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
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(sprintf(
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 %1$s\CoreController class is deprecated since version 3.36 and will be removed in 4.0.'
20
    .' Use %1$s\SearchAction or %1$s\DashboardAction instead.',
21
    __NAMESPACE__
22
), E_USER_DEPRECATED);
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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
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 sonata-project/admin-bundle 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(sprintf(
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 %s method is deprecated since 3.0 and will be removed in 4.0.'
80
            .' Inject the %s into the actions instead.',
81
            __METHOD__,
82
            Request::class
83
        ), E_USER_DEPRECATED);
84
85
        return $this->getCurrentRequest();
86
    }
87
88
    /**
89
     * @return Pool
90
     */
91
    protected function getAdminPool()
92
    {
93
        $pool = $this->container->get('sonata.admin.pool');
94
        \assert($pool instanceof Pool);
95
96
        return $pool;
97
    }
98
99
    /**
100
     * @return SearchHandler
101
     */
102
    protected function getSearchHandler()
103
    {
104
        $searchHandler = $this->get('sonata.admin.search.handler');
105
        \assert($searchHandler instanceof SearchHandler);
106
107
        return $searchHandler;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    protected function getBaseTemplate()
114
    {
115
        if ($this->getCurrentRequest()->isXmlHttpRequest()) {
116
            return $this->getTemplateRegistry()->getTemplate('ajax');
117
        }
118
119
        return $this->getTemplateRegistry()->getTemplate('layout');
120
    }
121
122
    private function getTemplateRegistry(): TemplateRegistryInterface
123
    {
124
        $templateRegistry = $this->container->get('sonata.admin.global_template_registry');
125
        \assert($templateRegistry instanceof TemplateRegistryInterface);
126
127
        return $templateRegistry;
128
    }
129
130
    private function getCurrentRequest(): Request
131
    {
132
        return $this->container->get('request_stack')->getCurrentRequest();
133
    }
134
}
135