Completed
Push — master ( 515575...4e0394 )
by Vytautas
9s
created

SettingsApiController::processBodyContent()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 9
nc 3
nop 1
crap 6
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 18
    public function __construct(CollectionsManager $settingsManager)
42
    {
43 18
        $this->settingsManager = $settingsManager;
44 18
    }
45
46
    /**
47
     * Get list of settings in the given collection
48
     *
49
     * @return JsonModel
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 JsonModel|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 JsonModel|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 8
                    continue;
90
                }
91
92 2
                return new ApiProblemResponse(new ApiProblem(400, 'Invalid parameters provided.'));
93 4
            }
94 6
        } catch (SettingDoesNotExistException $exception) {
95 2
            return new ApiProblemResponse(new ApiProblem(404, $exception->getMessage()));
96
        }
97
98 4
        foreach ($data as $key => $value) {
99 4
            $this->settings->setValue($key, $value);
100 4
        }
101
102 4
        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 JsonModel|ApiProblemResponse
112
     */
113 3
    public function update($name, $data)
114
    {
115
        try {
116 3
            if (!$this->settings->isValid($name, $data)) {
117 1
                return new ApiProblemResponse(new ApiProblem(400, 'Invalid parameters provided.'));
118
            }
119 1
            $this->settings->setValue($name, $data);
120 1
            return new JsonModel([$name => $data]);
121 1
        } 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 array $data
130
     *
131
     * @return JsonModel|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