Completed
Pull Request — master (#170)
by Simonas
03:06
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
        $repo = $this->get($this->getParameter('ongr_settings.repo'));
58
59
        /** @var DocumentIterator $result */
60
        $result = $repo->execute(
61
            (new Search())->addAggregation(new TermsAggregation('profiles', 'profile'))
62
        );
63
        /** @var AggregationValue $agg */
64
        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...
65
            $profiles[] = $agg->getValue('key');
66
        }
67
68
        return new JsonResponse($profiles);
69
    }
70
71
    /**
72
     * Returns a json list of profiles
73
     *
74
     * @param Request $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     *
76
     * @return Response
77
     */
78
    public function getFullProfilesAction()
79
    {
80
        $profiles = $this->get('ongr_settings.settings_manager')->getAllProfiles();
81
82
        return new JsonResponse(
83
            ['count' => count($profiles), 'documents' => $profiles]
84
        );
85
    }
86
87
    public function toggleProfileAction(Request $request)
88
    {
89
        $setting = $this->get('ongr_settings.settings_manager')
90
            ->get($this->getParameter('ongr_settings.active_profiles'));
91
92
        $name = $request->get('name');
93
94
        if ($setting) {
95
            $activeProfiles = $setting->getValue();
96
        } else {
97
            $activeProfiles = [];
98
        }
99
100
        $key = array_search($name, $activeProfiles);
101
        if ($key === false) {
102
            $activeProfiles[] = $name;
103
        } else {
104
            unset($activeProfiles[$key]);
105
        }
106
107
        $this->get('ongr_settings.settings_manager')->update($this->getParameter('ongr_settings.active_profiles'), [
108
            'value' => $activeProfiles
109
        ]);
110
111
        $this->get('ong_settings.cache_provider')->deleteAll();
112
113
        return new JsonResponse(['error' => false]);
114
    }
115
}
116