Completed
Pull Request — master (#170)
by Simonas
04:25
created

ProfilesController::listAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\ElasticsearchDSL\Aggregation\TopHitsAggregation;
18
use ONGR\ElasticsearchDSL\Search;
19
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue;
20
use ONGR\SettingsBundle\Document\Setting;
21
use ONGR\SettingsBundle\Service\SettingsManager;
22
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
27
/**
28
 * Class ProfileController. Placeholder for settings bundle profiles page.
29
 */
30
class ProfilesController extends Controller
31
{
32
    /**
33
     * Renders profiles page.
34
     *
35
     * @param Request $request
36
     *
37
     * @return Response
38
     */
39
    public function listAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        return $this->render(
42
            'ONGRSettingsBundle:Profiles:list.html.twig',
43
            []
44
        );
45
    }
46
47
    /**
48
     * Returns a json list of profiles
49
     *
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54
    public function getAllProfilesAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $profiles = [];
57
        /** @var Repository $repo */
58
        $repo = $this->get($this->getParameter('ongr_settings.repo'));
59
60
        /** @var DocumentIterator $result */
61
        $result = $repo->execute(
62
            ($repo->createSearch())->addAggregation(new TermsAggregation('profiles', 'profile'))
63
        );
64
        /** @var AggregationValue $agg */
65
        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...
66
            $profiles[] = $agg->getValue('key');
67
        }
68
69
        if (empty($profiles) || !in_array('default', $profiles)) array_unshift($profiles, 'default');
70
71
        return new JsonResponse($profiles);
72
    }
73
74
    /**
75
     * Returns a json list of profiles
76
     *
77
     * @return Response
78
     */
79
    public function getFullProfilesAction()
80
    {
81
        $profiles = $this->get('ongr_settings.settings_manager')->getAllProfiles();
82
83
        return new JsonResponse(
84
            ['count' => count($profiles), 'documents' => $profiles]
85
        );
86
    }
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' => $activeProfiles
116
        ]);
117
        $this->get('ong_settings.cache_provider')->deleteAll();
118
119
        return new JsonResponse(['error' => false]);
120
    }
121
}
122