Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

src/Action/SearchAction.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Action;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
18
use Sonata\AdminBundle\Admin\Pool;
19
use Sonata\AdminBundle\Search\SearchHandler;
20
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
21
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
22
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
27
final class SearchAction extends Controller
28
{
29
    /**
30
     * @var Pool
31
     */
32
    private $pool;
33
34
    /**
35
     * @var SearchHandler
36
     */
37
    private $searchHandler;
38
39
    /**
40
     * @var TemplateRegistryInterface
41
     */
42
    private $templateRegistry;
43
44
    /**
45
     * @var BreadcrumbsBuilderInterface
46
     */
47
    private $breadcrumbsBuilder;
48
49
    public function __construct(
50
        Pool $pool,
51
        SearchHandler $searchHandler,
52
        TemplateRegistryInterface $templateRegistry,
53
        BreadcrumbsBuilderInterface $breadcrumbsBuilder
54
    ) {
55
        $this->pool = $pool;
56
        $this->searchHandler = $searchHandler;
57
        $this->templateRegistry = $templateRegistry;
58
        $this->breadcrumbsBuilder = $breadcrumbsBuilder;
59
    }
60
61
    /**
62
     * The search action first render an empty page, if the query is set, then the template generates
63
     * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
64
     *
65
     * @throws \RuntimeException
66
     *
67
     * @return JsonResponse|Response
68
     */
69
    public function __invoke(Request $request): Response
70
    {
71
        if (!$request->get('admin') || !$request->isXmlHttpRequest()) {
72
            return $this->render($this->templateRegistry->getTemplate('search'), [
73
                'base_template' => $request->isXmlHttpRequest() ?
74
                    $this->templateRegistry->getTemplate('ajax') :
75
                    $this->templateRegistry->getTemplate('layout'),
76
                'breadcrumbs_builder' => $this->breadcrumbsBuilder,
77
                'admin_pool' => $this->pool,
78
                'query' => $request->get('q'),
79
                'groups' => $this->pool->getDashboardGroups(),
80
            ]);
81
        }
82
83
        try {
84
            $admin = $this->pool->getAdminByAdminCode($request->get('admin'));
85
        } catch (ServiceNotFoundException $e) {
86
            throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
87
        }
88
89
        if (!$admin instanceof AdminInterface) {
90
            throw new \RuntimeException('The requested service is not an Admin instance');
91
        }
92
93
        $results = [];
94
95
        $page = false;
96
        $total = false;
97
        if ($pager = $this->searchHandler->search(
98
            $admin,
99
            $request->get('q'),
100
            $request->get('page'),
101
            $request->get('offset')
102
        )) {
103
            foreach ($pager->getResults() as $result) {
104
                $results[] = [
105
                    'label' => $admin->toString($result),
106
                    'link' => $admin->getSearchResultLink($result),
107
                    'id' => $admin->id($result),
108
                ];
109
            }
110
            $page = (int) $pager->getPage();
111
            $total = (int) $pager->getNbResults();
0 ignored issues
show
The method getNbResults() does not exist on Sonata\AdminBundle\Datagrid\PagerInterface. Did you maybe mean getResults()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
        }
113
114
        $response = new JsonResponse([
115
            'results' => $results,
116
            'page' => $page,
117
            'total' => $total,
118
        ]);
119
        $response->setPrivate();
120
121
        return $response;
122
    }
123
}
124