Completed
Push — master ( bbff6f...c51acf )
by Rafał
19:14 queued 09:31
created

SettingsController::updateAction()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 34

Duplication

Lines 6
Ratio 17.65 %

Importance

Changes 0
Metric Value
dl 6
loc 34
rs 8.7537
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Settings Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\SettingsBundle\Controller;
18
19
use SWP\Bundle\SettingsBundle\Context\ScopeContextInterface;
20
use SWP\Bundle\SettingsBundle\Exception\InvalidScopeException;
21
use SWP\Bundle\SettingsBundle\Form\Type\BulkSettingsUpdateType;
22
use SWP\Bundle\SettingsBundle\Form\Type\SettingType;
23
use SWP\Component\Common\Response\ResponseContext;
24
use SWP\Component\Common\Response\SingleResourceResponse;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use Symfony\Component\Routing\Annotation\Route;
30
31
class SettingsController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

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...
32
{
33
    /**
34
     * Lists all settings.
35
     *
36
     * @ApiDoc(
37
     *     resource=true,
38
     *     description="Lists all settings",
39
     *     statusCodes={
40
     *         200="Returned on success."
41
     *     }
42
     * )
43
     * @Route("/api/{version}/settings/", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_settings_list")
44
     *
45
     * @return SingleResourceResponse
46
     */
47
    public function listAction()
48
    {
49
        $settingsManager = $this->get('swp_settings.manager.settings');
50
51
        return new SingleResourceResponse($settingsManager->all());
52
    }
53
54
    /**
55
     * Revert settings to defaults by scope.
56
     *
57
     * @ApiDoc(
58
     *     resource=true,
59
     *     description="Revert settings to defaults by scope",
60
     *     statusCodes={
61
     *         204="Returned on success."
62
     *     }
63
     * )
64
     * @Route("/api/{version}/settings/revert/{scope}", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_settings_revert")
65
     *
66
     * @return SingleResourceResponse
67
     */
68
    public function revertAction(string $scope)
69
    {
70
        $settingsManager = $this->get('swp_settings.manager.settings');
71
72
        $settingsManager->clearAllByScope($scope);
73
74
        return new SingleResourceResponse(null, new ResponseContext(204));
75
    }
76
77
    /**
78
     * Change setting value.
79
     *
80
     * @ApiDoc(
81
     *     resource=true,
82
     *     description="Change setting value",
83
     *     statusCodes={
84
     *         200="Returned on success.",
85
     *         404="Setting not found",
86
     *     },
87
     *     input="SWP\Bundle\SettingsBundle\Form\Type\SettingType"
88
     * )
89
     * @Route("/api/{version}/settings/", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_settings_update")
90
     *
91
     * @param Request $request
92
     *
93
     * @return SingleResourceResponse
94
     */
95
    public function updateAction(Request $request)
96
    {
97
        $form = $this->get('form.factory')->createNamed('', SettingType::class, [], [
98
            'method' => $request->getMethod(),
99
        ]);
100
101
        $form->handleRequest($request);
102
        if ($form->isSubmitted() && $form->isValid()) {
103
            $settingsManager = $this->get('swp_settings.manager.settings');
104
            $scopeContext = $this->get('swp_settings.context.scope');
105
            $data = $form->getData();
106
107
            $setting = $settingsManager->getOneSettingByName($data['name']);
108
109
            if (null === $setting) {
110
                throw new NotFoundHttpException('Setting with this name was not found.');
111
            }
112
113
            $scope = $setting['scope'];
114
            $owner = null;
115 View Code Duplication
            if (ScopeContextInterface::SCOPE_GLOBAL !== $scope) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                $owner = $scopeContext->getScopeOwner($scope);
117
                if (null === $owner) {
118
                    throw new InvalidScopeException($scope);
119
                }
120
            }
121
122
            $setting = $settingsManager->set($data['name'], $data['value'], $scope, $owner);
123
124
            return new SingleResourceResponse($setting);
125
        }
126
127
        return new SingleResourceResponse($form, new ResponseContext(400));
128
    }
129
130
    /**
131
     * Settings bulk update - update multiple settings.
132
     *
133
     * @ApiDoc(
134
     *     resource=true,
135
     *     description="Settings bulk update",
136
     *     statusCodes={
137
     *         200="Returned on success.",
138
     *         404="Setting not found",
139
     *     },
140
     *     input="SWP\Bundle\SettingsBundle\Form\Type\BulkSettingsUpdateType"
141
     * )
142
     * @Route("/api/{version}/settings/bulk/", methods={"PATCH"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_settings_bulk_update")
143
     *
144
     * @param Request $request
145
     *
146
     * @return SingleResourceResponse
147
     */
148
    public function bulkAction(Request $request)
149
    {
150
        $form = $this->get('form.factory')->createNamed('', BulkSettingsUpdateType::class, [], [
151
            'method' => $request->getMethod(),
152
        ]);
153
154
        $form->handleRequest($request);
155
        if ($form->isSubmitted() && $form->isValid()) {
156
            $settingsManager = $this->get('swp_settings.manager.settings');
157
            $scopeContext = $this->get('swp_settings.context.scope');
158
            $data = $form->getData();
159
160
            foreach ((array) $data['bulk'] as $item) {
161
                $setting = $settingsManager->getOneSettingByName($item['name']);
162
                if (null === $setting) {
163
                    throw new NotFoundHttpException(sprintf('Setting with "%s" name was not found.', $item['name']));
164
                }
165
166
                $scope = $setting['scope'];
167
                $owner = null;
168 View Code Duplication
                if (ScopeContextInterface::SCOPE_GLOBAL !== $scope) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
                    $owner = $scopeContext->getScopeOwner($scope);
170
                    if (null === $owner) {
171
                        throw new InvalidScopeException($scope);
172
                    }
173
                }
174
175
                $settingsManager->set($item['name'], $item['value'], $scope, $owner);
176
            }
177
178
            return new SingleResourceResponse($settingsManager->all());
179
        }
180
181
        return new SingleResourceResponse($form, new ResponseContext(400));
182
    }
183
}
184