Completed
Push — master ( 60736e...19ad97 )
by Simonas
02:31
created

ProfilesController::getAllProfilesAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 11
nc 4
nop 0
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\SettingsBundle\Controller;
13
14
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
15
use ONGR\ElasticsearchBundle\Service\Repository;
16
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;
17
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Class ProfileController. Placeholder for settings bundle profiles page.
25
 */
26
class ProfilesController extends Controller
27
{
28
    /**
29
     * Renders profiles page.
30
     *
31
     * @return Response
32
     */
33
    public function listAction()
34
    {
35
        return $this->render('ONGRSettingsBundle:Profiles:list.html.twig');
36
    }
37
38
    /**
39
     * Returns a json list of profiles
40
     *
41
     * @return JsonResponse
42
     */
43
    public function getAllProfilesAction()
44
    {
45
        $profiles = [];
46
        /** @var Repository $repo */
47
        $repo = $this->get($this->getParameter('ongr_settings.repo'));
48
49
        $search = $repo->createSearch();
50
        $search->addAggregation(new TermsAggregation('profiles', 'profile'));
51
52
        /** @var DocumentIterator $result */
53
        $result = $repo->execute($search);
54
55
        /** @var AggregationValue $agg */
56
        foreach ($result->getAggregation('profiles') as $agg) {
0 ignored issues
show
Bug introduced by
The expression $result->getAggregation('profiles') of type null|object<ONGR\Elastic...ation\AggregationValue> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
57
            $profiles[] = $agg->getValue('key');
58
        }
59
60
        if (empty($profiles) || !in_array('default', $profiles)) {
61
            array_unshift($profiles, 'default');
62
        }
63
64
        return new JsonResponse($profiles);
65
    }
66
67
    /**
68
     * Returns a json list of profiles
69
     *
70
     * @return JsonResponse
71
     */
72
    public function getFullProfilesAction()
73
    {
74
        $profiles = $this->get('ongr_settings.settings_manager')->getAllProfiles();
75
76
        return new JsonResponse(
77
            ['count' => count($profiles), 'documents' => $profiles]
78
        );
79
    }
80
81
    /**
82
     * Toggle profile activation.
83
     *
84
     * @param Request $request
85
     *
86
     * @return JsonResponse
87
     */
88
    public function toggleProfileAction(Request $request)
89
    {
90
        $settingName = $this->getParameter('ongr_settings.active_profiles');
91
        $manager = $this->get('ongr_settings.settings_manager');
92
93
        #TODO Not sure where is the right place to put active profiles setting initiating
94
        if (!$manager->has($settingName)) {
95
            $manager->create(
96
                [
97
                    'name' => $settingName,
98
                    'value' => [],
99
                    'type' => 'hidden',
100
                ]
101
            );
102
        }
103
104
        $profileName = $request->get('name');
105
        $activeProfiles = (array)$manager->getValue($settingName, []);
106
107
        $key = array_search($profileName, $activeProfiles);
108
        if ($key === false) {
109
            $activeProfiles[] = $profileName;
110
        } else {
111
            unset($activeProfiles[$key]);
112
        }
113
114
        $manager->update($settingName, [
115
            'value' => array_values($activeProfiles)
116
        ]);
117
118
        $this->get('ong_settings.cache_provider')->deleteAll();
119
120
        return new JsonResponse(['error' => false]);
121
    }
122
}
123