Completed
Push — master ( 071097...7934b9 )
by
unknown
09:17
created

HelperController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Controller;
4
5
use MediaMonks\SonataMediaBundle\Admin\MediaAdmin;
6
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Templating\EngineInterface;
11
12
class HelperController
13
{
14
    /**
15
     * @var MediaAdmin
16
     */
17
    private $mediaAdmin;
18
19
    /**
20
     * @var EngineInterface
21
     */
22
    private $templating;
23
24
    /**
25
     * @param MediaAdmin $mediaAdmin
26
     * @param EngineInterface $templating
27
     */
28 1
    public function __construct(MediaAdmin $mediaAdmin, EngineInterface $templating)
29
    {
30 1
        $this->mediaAdmin = $mediaAdmin;
31 1
        $this->templating = $templating;
32 1
    }
33
34
    /**
35
     * @param Request $request
36
     * @return JsonResponse
37
     */
38 1 View Code Duplication
    public function getAutocompleteItemsAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40 1
        $this->mediaAdmin->checkAccess('list');
41
42 1
        $minimumInputLength = 3;
43 1
        $searchText = $request->get('q');
44 1
        if (mb_strlen($searchText, 'UTF-8') < $minimumInputLength) {
45 1
            return new JsonResponse(['status' => 'KO', 'message' => 'Too short search string'], Response::HTTP_FORBIDDEN);
46
        }
47
48 1
        $this->mediaAdmin->setPersistFilters(false);
49 1
        $datagrid = $this->mediaAdmin->getDatagrid();
50 1
        $datagrid->setValue('title', null, $searchText);
51 1
        $datagrid->setValue('_per_page', null, $request->query->get('_per_page', 10));
52 1
        $datagrid->setValue('_page', null, $request->query->get('_page', 1));
53 1
        $datagrid->buildPager();
54
55 1
        $pager = $datagrid->getPager();
56 1
        $results = $pager->getResults();
57
58
        /**
59
         * @var MediaInterface $media
60
         */
61 1
        $items = [];
62 1
        foreach($results as $media) {
63 1
            $items[] = [
64 1
                'id' => $media->getId(),
65 1
                'label' => $this->templating->render('@MediaMonksSonataMedia/CRUD/autocomplete.html.twig', [
66 1
                    'media' => $media
67
                ])
68
            ];
69
        }
70
71 1
        return new JsonResponse([
72 1
            'status' => 'OK',
73
            'more' => false,
74 1
            'items' => $items
75
        ]);
76
    }
77
}
78