Completed
Push — develop ( ddf000...96b89c )
by Kevin
10:14 queued 06:44
created

Save   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 67
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A execute() 0 20 3
A process() 0 8 2
B read() 0 20 5
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