1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Svycka\Settings\Controller; |
4
|
|
|
|
5
|
|
|
use Svycka\Settings\Collection\CollectionInterface; |
6
|
|
|
use Svycka\Settings\Collection\CollectionsManager; |
7
|
|
|
use Svycka\Settings\Exception\SettingDoesNotExistException; |
8
|
|
|
use Zend\Mvc\Controller\AbstractRestfulController; |
9
|
|
|
use Zend\Mvc\MvcEvent; |
10
|
|
|
use Zend\Stdlib\ArrayUtils; |
11
|
|
|
use Zend\View\Model\JsonModel; |
12
|
|
|
use ZF\ApiProblem\ApiProblem; |
13
|
|
|
use ZF\ApiProblem\ApiProblemResponse; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Vytautas Stankus <[email protected]> |
17
|
|
|
* @license MIT |
18
|
|
|
*/ |
19
|
|
|
final class SettingsApiController extends AbstractRestfulController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Name of request or query parameter containing identifier |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $identifierName = 'name'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CollectionsManager |
30
|
|
|
*/ |
31
|
|
|
protected $settingsManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CollectionInterface |
35
|
|
|
*/ |
36
|
|
|
protected $settings; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param CollectionsManager $settingsManager |
40
|
|
|
*/ |
41
|
17 |
|
public function __construct(CollectionsManager $settingsManager) |
42
|
|
|
{ |
43
|
17 |
|
$this->settingsManager = $settingsManager; |
44
|
17 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get list of settings in the given collection |
48
|
|
|
* |
49
|
|
|
* @return array|ApiProblemResponse |
50
|
|
|
*/ |
51
|
1 |
|
public function getList() |
52
|
|
|
{ |
53
|
1 |
|
return new JsonModel($this->settings->getList()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get one setting by name from collection |
58
|
|
|
* |
59
|
|
|
* @param mixed $name |
60
|
|
|
* |
61
|
|
|
* @return array|ApiProblemResponse |
62
|
|
|
*/ |
63
|
2 |
|
public function get($name) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
2 |
|
$value = $this->settings->getValue($name); |
67
|
1 |
|
return new JsonModel([$name => $value]); |
68
|
1 |
|
} catch (SettingDoesNotExistException $exception) { |
69
|
1 |
|
return new ApiProblemResponse(new ApiProblem(404, $exception->getMessage())); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create or update settings in collection |
75
|
|
|
* |
76
|
|
|
* @param array $data |
77
|
|
|
* |
78
|
|
|
* @return mixed|ApiProblemResponse |
79
|
|
|
*/ |
80
|
10 |
|
public function create($data) |
81
|
|
|
{ |
82
|
10 |
|
if (!ArrayUtils::isHashTable($data)) { |
83
|
2 |
|
return new ApiProblemResponse(new ApiProblem(400, 'Data should be array of key => value pairs.')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
try { |
87
|
8 |
|
foreach ($data as $key => $value) { |
88
|
8 |
|
if ($this->settings->isValid($key, $value)) { |
89
|
2 |
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new ApiProblemResponse(new ApiProblem(400, 'Invalid parameters provided.')); |
93
|
|
|
} |
94
|
8 |
|
} catch (SettingDoesNotExistException $exception) { |
95
|
2 |
|
return new ApiProblemResponse(new ApiProblem(404, $exception->getMessage())); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($data as $key => $value) { |
99
|
|
|
$this->settings->setValue($key, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new JsonModel($data); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Update one setting in collection by setting name |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* @param mixed $data |
110
|
|
|
* |
111
|
|
|
* @return ApiProblemResponse |
112
|
|
|
*/ |
113
|
3 |
|
public function update($name, $data) |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
3 |
|
if (!$this->settings->isValid($name, $data)) { |
117
|
|
|
return new ApiProblemResponse(new ApiProblem(400, 'Invalid parameters provided.')); |
118
|
|
|
} |
119
|
|
|
$this->settings->setValue($name, $data); |
120
|
|
|
return new JsonModel([$name => $data]); |
121
|
3 |
|
} catch (SettingDoesNotExistException $exception) { |
122
|
1 |
|
return new ApiProblemResponse(new ApiProblem(404, $exception->getMessage())); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Create or update settings in collection |
128
|
|
|
* |
129
|
|
|
* @param mixed $data |
130
|
|
|
* |
131
|
|
|
* @return mixed|ApiProblemResponse |
132
|
|
|
*/ |
133
|
5 |
|
public function replaceList($data) |
134
|
|
|
{ |
135
|
5 |
|
return $this->create($data); |
136
|
|
|
} |
137
|
|
|
|
138
|
17 |
|
public function onDispatch(MvcEvent $event) |
139
|
|
|
{ |
140
|
17 |
|
$collection = $this->params()->fromRoute('collection'); |
141
|
|
|
|
142
|
17 |
|
if (!$this->settingsManager->has($collection)) { |
143
|
1 |
|
return new ApiProblemResponse(new ApiProblem(404, 'Settings collection not found.')); |
144
|
|
|
} |
145
|
|
|
|
146
|
16 |
|
$this->settings = $this->settingsManager->get($collection); |
147
|
|
|
|
148
|
16 |
|
return parent::onDispatch($event); |
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* workaround: for https://github.com/zendframework/zend-mvc/issues/42 |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
8 |
|
protected function processBodyContent($request) |
155
|
|
|
{ |
156
|
8 |
|
$content = $request->getContent(); |
157
|
|
|
|
158
|
|
|
// JSON content? decode and return it. |
159
|
8 |
|
if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) { |
160
|
1 |
|
return json_decode($content, $this->jsonDecodeType); |
161
|
|
|
} |
162
|
|
|
|
163
|
7 |
|
parse_str($content, $parsedParams); |
164
|
|
|
|
165
|
|
|
// If parse_str fails to decode, or we have a single element with empty value |
166
|
7 |
|
if (!is_array($parsedParams) || empty($parsedParams) |
167
|
7 |
|
|| (1 == count($parsedParams) && '' === reset($parsedParams)) |
168
|
7 |
|
) { |
169
|
4 |
|
return $content; |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
return $parsedParams; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|