Completed
Push — master ( 5bd517...c7b86e )
by Tomáš
08:52
created

Configuration::addGlobalVarialbe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Configuration;
11
12
use SplFileInfo;
13
use Symplify\PHP7_Sculpin\Configuration\Parser\YamlAndNeonParser;
14
15
final class Configuration
16
{
17
    /**
18
     * @var string
19
     */
20
    const DEFAULT_POST_ROUTE = 'blog/:year/:month/:day/:title';
21
22
    /**
23
     * @var array
24
     */
25
    private $globalVariables = [];
26
27 11
    /**
28
     * @var YamlAndNeonParser
29 11
     */
30 11
    private $yamlAndNeonParser;
31
32
    /**
33
     * @var string
34
     */
35 3
    private $sourceDirectory;
36
37 3
    /**
38 2
     * @var string
39 2
     */
40
    private $outputDirectory;
41 3
42
    /**
43
     * @var string
44
     */
45
    private $postRoute = self::DEFAULT_POST_ROUTE;
46
47 2
    public function __construct(YamlAndNeonParser $yamlAndNeonParser)
48
    {
49 2
        $this->yamlAndNeonParser = $yamlAndNeonParser;
50 2
    }
51
52 9
    /**
53
     * @param SplFileInfo[] $files
54 9
     */
55
    public function loadFromFiles(array $files)
56
    {
57
        foreach ($files as $file) {
58
            $decodedOptions = $this->yamlAndNeonParser->decodeFromFile($file->getRealPath());
59
            $this->globalVariables += $this->extractPostRoute($decodedOptions);
60
        }
61
    }
62
63
    /**
64
     * @param string       $name
65
     * @param string|array $value
66
     */
67
    public function addGlobalVarialbe(string $name, $value)
68
    {
69
        $this->globalVariables[$name] = $value;
70
    }
71
72
    public function getGlobalVariables() : array
73
    {
74
        return $this->globalVariables;
75
    }
76
77
    public function setSourceDirectory(string $sourceDirectory)
78
    {
79
        $this->sourceDirectory = $sourceDirectory;
80
    }
81
82
    public function getSourceDirectory() : string
83
    {
84
        return $this->sourceDirectory;
85
    }
86
87
    public function setOutputDirectory(string $outputDirectory)
88
    {
89
        $this->outputDirectory = $outputDirectory;
90
    }
91
92
    public function getOutputDirectory() : string
93
    {
94
        return $this->outputDirectory;
95
    }
96
97
    public function setPostRoute(string $postRoute)
98
    {
99
        $this->postRoute = $postRoute;
100
    }
101
102
    public function getPostRoute() : string
103
    {
104
        return $this->postRoute;
105
    }
106
107
    private function extractPostRoute(array $options) : array
108
    {
109
        if (!isset($options['configuration']['postRoute'])) {
110
            return $options;
111
        }
112
113
        $this->setPostRoute($options['configuration']['postRoute']);
114
        unset($options['configuration']['postRoute']);
115
116
        return $options;
117
    }
118
}
119