Completed
Push — master ( c38081...6a9f65 )
by Simonas
24:29
created

Controller/ProfilesController.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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'));
0 ignored issues
show
Deprecated Code introduced by
The class ONGR\ElasticsearchDSL\Aggregation\TermsAggregation has been deprecated with message: Aggregations was moved to it's type namespace. Add `Metric` or `Bucketing` after `Aggregation`. This class will be removed in 3.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
51
52
        /** @var DocumentIterator $result */
53
        $result = $repo->execute($search);
0 ignored issues
show
Deprecated Code introduced by
The method ONGR\ElasticsearchBundle...e\Repository::execute() has been deprecated with message: Use strict execute functions instead. e.g. executeIterator, executeRawIterator.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
55
        /** @var AggregationValue $agg */
56
        foreach ($result->getAggregation('profiles') as $agg) {
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