1 | <?php |
||
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) |
|
45 | |||
46 | /** |
||
47 | * Get list of settings in the given collection |
||
48 | * |
||
49 | * @return array|ApiProblemResponse |
||
50 | */ |
||
51 | 1 | public function getList() |
|
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) |
|
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) |
|
137 | |||
138 | 17 | public function onDispatch(MvcEvent $event) |
|
150 | /** |
||
151 | * workaround: for https://github.com/zendframework/zend-mvc/issues/42 |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 8 | protected function processBodyContent($request) |
|
174 | } |
||
175 |