Completed
Push — master ( 229d16...5190dc )
by Simonas
122:26 queued 57:21
created

ListController::buildLocalesList()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.2
cc 4
eloc 12
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\TranslationsBundle\Controller;
13
14
use ONGR\ElasticsearchBundle\Result\Result;
15
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;
16
use ONGR\ElasticsearchBundle\Service\Repository;
17
use ONGR\FilterManagerBundle\Filter\ViewData;
18
use ONGR\FilterManagerBundle\Search\SearchResponse;
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Controller used for displaying translations.
25
 */
26
class ListController extends Controller
27
{
28
    /**
29
     * @var Repository
30
     */
31
    private $repository;
32
33
    /**
34
     * Injects elasticsearch repository for listing actions.
35
     *
36
     * @param Repository $repository Elasticsearch repository.
37
     */
38
    public function __construct(Repository $repository)
39
    {
40
        $this->repository = $repository;
41
    }
42
43
    /**
44
     * Renders view with filter manager response.
45
     *
46
     * @param Request $request Request.
47
     *
48
     * @return Response
49
     */
50
    public function listAction(Request $request)
51
    {
52
        /** @var SearchResponse $fmr */
53
        $fmr = $this->get('ongr_translations.filter_manager')->handleRequest($request);
54
55
        return $this->render(
56
            'ONGRTranslationsBundle:List:list.html.twig',
57
            [
58
                'data' => iterator_to_array($fmr->getResult()),
59
                'locales' => $this->buildLocalesList($fmr->getFilters()['locale']),
0 ignored issues
show
Compatibility introduced by
$fmr->getFilters()['locale'] of type object<ONGR\FilterManagerBundle\Filter\ViewData> is not a sub-type of object<ONGR\FilterManage...a\ChoicesAwareViewData>. It seems like you assume a child class of the class ONGR\FilterManagerBundle\Filter\ViewData to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
                'filters_manager' => $fmr,
61
            ]
62
        );
63
    }
64
65
    /**
66
     * Creates locales list.
67
     *
68
     * @param ViewData\ChoicesAwareViewData $filter
69
     *
70
     * @return array
71
     */
72
    private function buildLocalesList($filter)
73
    {
74
        $locales = $this->container->getParameter('ongr_translations.managed_locales');
75
        $list = [];
76
        foreach ($locales as $locale) {
77
            $list[$locale] = true;
78
        }
79
        ksort($list);
80
        $activeLocales = [];
81
82
        if ($filter->getState()->isActive()) {
83
            foreach ($filter->getChoices() as $choice) {
84
                $activeLocales[$choice->getLabel()] = $choice->isActive();
85
            }
86
            $list = array_merge($list, $activeLocales);
87
        }
88
89
        return $list;
90
    }
91
}
92