Rebuild::getContexts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\File\Context\AbstractContextConfigurationFile;
12
use Magium\Configuration\View\Controllers\Helpers\ContextRepository;
13
use Magium\Configuration\View\ViewConfiguration;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Zend\View\Model\JsonModel;
16
17
class Rebuild implements ControllerInterface
18
{
19
20
    const STATUS_SUCCESS = 'success';
21
    const STATUS_ERROR = 'error';
22
23
    protected $builder;
24
    protected $configurationFile;
25
26
    public function __construct(
27
        BuilderInterface $builder,
28
        AbstractContextConfigurationFile $configurationFile
29
    )
30
    {
31
        $this->builder = $builder;
32
        $this->configurationFile = $configurationFile;
33
    }
34
35
    public function execute(ServerRequestInterface $request)
36
    {
37
        $messages = [];
38
        try {
39
            $contexts = $this->getContexts();
40
            foreach ($contexts as $context => $name) {
41
                $this->builder->build($context);
42
                $messages[] = [
43
                    'status' => self::STATUS_SUCCESS,
44
                    'message' => sprintf('Context "%s (%s)" rebuilt', $context, $name)
45
                ];
46
            }
47
        } catch (\Exception $e) {
48
            $messages[] = [
49
                'status'    => self::STATUS_ERROR,
50
                'message'   => $e->getMessage()
51
            ];
52
        }
53
        return new JsonModel($messages);
54
    }
55
56
    public function getContexts()
57
    {
58
        return (new ContextRepository($this->configurationFile))->getContextList();
59
    }
60
}
61