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\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
24
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
25
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
26
|
|
|
use SWP\Component\Common\Response\SingleResourceResponseInterface; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
28
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
31
|
|
|
|
32
|
|
|
class SettingsController extends AbstractController |
33
|
|
|
{ |
34
|
|
|
protected $settingsManager; |
35
|
|
|
|
36
|
|
|
protected $scopeContext; |
37
|
|
|
|
38
|
|
|
protected $formFactory; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
SettingsManagerInterface $settingsManager, |
42
|
|
|
ScopeContextInterface $scopeContext, |
43
|
|
|
FormFactoryInterface $formFactory |
44
|
|
|
) { |
45
|
|
|
$this->settingsManager = $settingsManager; |
46
|
|
|
$this->scopeContext = $scopeContext; |
47
|
|
|
$this->formFactory = $formFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function list(): SingleResourceResponseInterface |
51
|
|
|
{ |
52
|
|
|
return new SingleResourceResponse($this->settingsManager->all()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function revert(string $scope): SingleResourceResponseInterface |
56
|
|
|
{ |
57
|
|
|
$this->settingsManager->clearAllByScope($scope); |
58
|
|
|
|
59
|
|
|
return new SingleResourceResponse(null, new ResponseContext(204)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function update(Request $request): SingleResourceResponseInterface |
63
|
|
|
{ |
64
|
|
|
$form = $this->formFactory->createNamed('', SettingType::class, [], [ |
65
|
|
|
'method' => $request->getMethod(), |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$form->handleRequest($request); |
69
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
70
|
|
|
$data = $form->getData(); |
71
|
|
|
|
72
|
|
|
$setting = $this->settingsManager->getOneSettingByName($data['name']); |
73
|
|
|
|
74
|
|
|
if (null === $setting) { |
75
|
|
|
throw new NotFoundHttpException('Setting with this name was not found.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$scope = $setting['scope']; |
79
|
|
|
$owner = null; |
80
|
|
View Code Duplication |
if (ScopeContextInterface::SCOPE_GLOBAL !== $scope) { |
|
|
|
|
81
|
|
|
$owner = $this->scopeContext->getScopeOwner($scope); |
82
|
|
|
if (null === $owner) { |
83
|
|
|
throw new InvalidScopeException($scope); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$setting = $this->settingsManager->set($data['name'], $data['value'], $scope, $owner); |
88
|
|
|
|
89
|
|
|
return new SingleResourceResponse($setting); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function bulk(Request $request): SingleResourceResponseInterface |
96
|
|
|
{ |
97
|
|
|
$form = $this->formFactory->createNamed('', BulkSettingsUpdateType::class, [], [ |
98
|
|
|
'method' => $request->getMethod(), |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$form->handleRequest($request); |
102
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
103
|
|
|
$data = $form->getData(); |
104
|
|
|
|
105
|
|
|
foreach ((array) $data['bulk'] as $item) { |
106
|
|
|
$setting = $this->settingsManager->getOneSettingByName($item['name']); |
107
|
|
|
if (null === $setting) { |
108
|
|
|
throw new NotFoundHttpException(sprintf('Setting with "%s" name was not found.', $item['name'])); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$scope = $setting['scope']; |
112
|
|
|
$owner = null; |
113
|
|
View Code Duplication |
if (ScopeContextInterface::SCOPE_GLOBAL !== $scope) { |
|
|
|
|
114
|
|
|
$owner = $this->scopeContext->getScopeOwner($scope); |
115
|
|
|
if (null === $owner) { |
116
|
|
|
throw new InvalidScopeException($scope); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->settingsManager->set($item['name'], $item['value'], $scope, $owner); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return new SingleResourceResponse($this->settingsManager->all()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.