Completed
Pull Request — develop (#61)
by Kevin
09:23
created

Save::read()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
crap 30
1
<?php
2
3
namespace Magium\Configuration\View\Controllers;
4
5
use Interop\Container\ContainerInterface;
6
use Magium\Configuration\Config\BuilderInterface;
7
use Magium\Configuration\Config\Repository\ConfigInterface;
8
use Magium\Configuration\Config\MergedStructure;
9
use Magium\Configuration\Config\Repository\ConfigurationRepository;
10
use Magium\Configuration\Config\Storage\StorageInterface;
11
use Magium\Configuration\View\ViewConfiguration;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Zend\View\Model\JsonModel;
14
15
class Save implements ControllerInterface
16
{
17
18
    const STATUS_SUCCESS = 'success';
19
    const STATUS_ERROR = 'error';
20
21
    protected $builder;
22
23
    public function __construct(
24
        BuilderInterface $builder
25
    )
26
    {
27
        $this->builder = $builder;
28
    }
29
30
    public function execute(ServerRequestInterface $request)
31
    {
32
        try {
33
            $json = $this->read($request);
34
            $this->process($json);
35
            return new JsonModel([[
36
                'status'    => self::STATUS_SUCCESS,
37
                'message'   => sprintf(
38
                    '%d setting%s saved in the context: %s',
39
                        count($json['values']),
40
                        count($json['values'])>1?'s':'',
41
                        $json['context'])
42
            ]]);
43
        } catch (\Exception $e) {
44
            return new JsonModel([[
45
                'status'    => self::STATUS_ERROR,
46
                'message'   => $e->getMessage()
47
            ]]);
48
        }
49
    }
50
51
    public function process(array $json)
52
    {
53
        $context = $json['context'];
54
55
        foreach ($json['values'] as $path => $value) {
56
            $this->builder->setValue($path, $value, $context);
57
        }
58
    }
59
60
    public function read(ServerRequestInterface $request)
61
    {
62
        if (strpos($request->getHeader('content-type'),  'application/json') === false) {
63
            throw new InvalidRequestException('MCM save operation requires an application/json content type');
64
        }
65
        $json  = json_decode($request->getBody()->getContents(), true);
66
        if ($json === false) {
67
            throw new InvalidRequestException('Unable to read JSON string');
68
        }
69
70
        if (!isset($json['values'])) {
71
            throw new InvalidRequestException('Missing required values key');
72
        }
73
74
        if (!isset($json['context'])) {
75
            $json['context'] = ConfigInterface::CONTEXT_DEFAULT;
76
        }
77
78
        return $json;
79
    }
80
81
}
82