Completed
Push — master ( 660487...67b4aa )
by
unknown
06:25
created

getAutocompleteItemsAction()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 52
ccs 0
cts 37
cp 0
rs 8.6868
c 1
b 0
f 0
cc 5
eloc 29
nc 4
nop 1
crap 30

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Controller;
4
5
use MediaMonks\SonataMediaBundle\Entity\Media;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
10
11
class MediaSearchController extends Controller
12
{
13
    /**
14
     * @param Request $request
15
     * @return \Symfony\Component\HttpFoundation\JsonResponse
16
     */
17
    public function getAutocompleteItemsAction(Request $request)
18
    {
19
        $targetAdmin = $this->get('sonata.admin.pool')->getInstance($request->get('admin_code'));
20
        if (!$targetAdmin->isGranted('CREATE') && !$targetAdmin->isGranted('EDIT')) {
21
            throw new AccessDeniedHttpException();
22
        }
23
24
        $mediaAdmin = $this->get('mediamonks.sonata_media.admin.media');
25
        $mediaAdmin->checkAccess('list');
26
27
        $minimumInputLength = 3;
28
        $searchText = $request->get('q');
29
        if (mb_strlen($searchText, 'UTF-8') < $minimumInputLength) {
30
            return $this->json(['status' => 'KO', 'message' => 'Too short search string'], Response::HTTP_FORBIDDEN);
31
        }
32
33
        /*$entityManager = $this->get('doctrine.orm.entity_manager');
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
        $query = $entityManager->getRepository('MediaMonksSonataMediaBundle:Media')->search($searchText);
35
        $query->setMaxResults($request->query->getInt('_per_page'));
36
        $query->setFirstResult(($request->query->getInt('_page') - 1) * $request->query->getInt('_per_page'));*/
37
38
        $mediaAdmin->setPersistFilters(false);
39
        $datagrid = $mediaAdmin->getDatagrid();
40
        $datagrid->setValue('title', null, $searchText);
41
        $datagrid->setValue('type', null, 'image');
42
        $datagrid->setValue('_per_page', null, 100);
43
        $datagrid->setValue('_page', null, $request->query->get(1, 1));
44
        $datagrid->buildPager();
45
46
        $pager = $datagrid->getPager();
47
48
        $results = $pager->getResults();
49
50
        /**
51
         * @var Media $media
52
         */
53
        $items = [];
54
        foreach($results as $media) {
55
            $items[] = [
56
                'id' => $media->getId(),
57
                'label' => $this->get('templating')->render('@MediaMonksSonataMedia/MediaAdmin/autocomplete.html.twig', [
58
                    'media' => $media
59
                ])
60
            ];
61
        }
62
63
        return $this->json([
64
            'status' => 'OK',
65
            'more' => false,
66
            'items' => $items
67
        ]);
68
    }
69
}
70