Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

CoreController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
D searchAction() 0 45 9
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Controller;
13
14
use Sonata\AdminBundle\Admin\AdminInterface;
15
use Sonata\AdminBundle\Controller\CoreController as BaseCoreController;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Extension of Sonata CoreController.
21
 *
22
 * @author Marcos Bezerra de Menezes <[email protected]>
23
 */
24
class CoreController extends BaseCoreController
25
{
26
    /**
27
     * @param Request $request
28
     *
29
     * @return JsonResponse|Response
30
     *
31
     * @throws \RuntimeException
32
     */
33
    public function searchAction(Request $request)
34
    {
35
        if ($request->get('admin') && $request->isXmlHttpRequest()) {
36
            try {
37
                $admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
38
            } catch (ServiceNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class Blast\Bundle\CoreBundle\...erviceNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
39
                throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
40
            }
41
42
            if (!$admin instanceof AdminInterface) {
43
                throw new \RuntimeException('The requested service is not an Admin instance');
44
            }
45
46
            $handler = $this->getSearchHandler();
47
48
            $results = array();
49
50
            if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
51
                foreach ($pager->getResults() as $result) {
52
                    $results[] = array(
53
                        'label' => $admin->toString($result),
54
                        'link'  => $admin->generateObjectUrl('show', $result),  // Sonata uses "edit", we prefer "show"
55
                        'id'    => $admin->id($result),
56
                    );
57
                }
58
            }
59
60
            $response = new JsonResponse(array(
61
                'results' => $results,
62
                'page'    => $pager ? (int) $pager->getPage() : false,
63
                'total'   => $pager ? (int) $pager->getNbResults() : false,
0 ignored issues
show
Bug introduced by
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...
64
            ));
65
            $response->setPrivate();
66
67
            return $response;
68
        }
69
70
        return $this->render($this->container->get('sonata.admin.pool')->getTemplate('search'), array(
71
            'base_template'       => $this->getBaseTemplate(),
72
            'breadcrumbs_builder' => $this->get('sonata.admin.breadcrumbs_builder'),
73
            'admin_pool'          => $this->container->get('sonata.admin.pool'),
74
            'query'               => $request->get('q'),
75
            'groups'              => $this->getAdminPool()->getDashboardGroups(),
76
        ));
77
    }
78
}
79