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

HelperController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 59.09 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 39
loc 66
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getAutocompleteItemsAction() 39 39 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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