Completed
Push — master ( 9c95f5...e428dc )
by Paweł
06:06
created

SettingsController::updateAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
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\SettingType;
22
use SWP\Component\Common\Response\SingleResourceResponse;
23
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
28
use SWP\Component\Common\Response\ResourcesListResponse;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
31
32
class SettingsController extends Controller
33
{
34
    /**
35
     * Lists all settings.
36
     *
37
     * @ApiDoc(
38
     *     resource=true,
39
     *     description="Lists all settings",
40
     *     statusCodes={
41
     *         200="Returned on success."
42
     *     }
43
     * )
44
     * @Route("/api/{version}/settings/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_settings_list")
45
     * @Method("GET")
46
     * Cache(expires="10 minutes", public=true)
47
     *
48
     * @return ResourcesListResponse
49
     */
50
    public function listAction()
51
    {
52
        $settingsManager = $this->get('swp_settings.manager.settings');
53
54
        return new SingleResourceResponse($settingsManager->all());
55
    }
56
57
    /**
58
     * Change setting value.
59
     *
60
     * @ApiDoc(
61
     *     resource=true,
62
     *     description="Change setting value",
63
     *     statusCodes={
64
     *         201="Returned on success.",
65
     *         404="Setting not found",
66
     *     },
67
     *     input="SWP\Bundle\SettingsBundle\Form\Type\SettingType"
68
     * )
69
     * @Route("/api/{version}/settings/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_settings_update")
70
     * @Method("PATCH")
71
     *
72
     * @param Request $request
73
     *
74
     * @return SingleResourceResponse
75
     */
76
    public function updateAction(Request $request)
77
    {
78
        $form = $this->createForm(SettingType::class, [], [
79
            'method' => $request->getMethod(),
80
        ]);
81
82
        $form->handleRequest($request);
83
        if ($form->isValid()) {
84
            $settingsManager = $this->get('swp_settings.manager.settings');
85
            $scopeContext = $this->get('swp_settings.context.scope');
86
            $data = $form->getData();
87
88
            if (!array_key_exists($data['name'], $settingsManager->all())) {
89
                throw new NotFoundHttpException('Setting with this name was not found.');
90
            }
91
92
            $setting = $settingsManager->all()[$data['name']];
93
            $scope = $setting['scope'];
94
            $owner = null;
95
            if ($scope !== ScopeContextInterface::SCOPE_GLOBAL) {
96
                $owner = $scopeContext->getScopeOwner($scope);
97
                if (null === $owner) {
98
                    throw new InvalidScopeException($scope);
99
                }
100
            }
101
102
            $setting = $settingsManager->set($data['name'], $data['value'], $scope, $owner);
103
104
            return new SingleResourceResponse($setting);
105
        }
106
107
        return new SingleResourceResponse($form);
108
    }
109
}
110