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

MediaSearchController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 59
ccs 0
cts 37
cp 0
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getAutocompleteItemsAction() 0 52 5
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