Passed
Push — master ( da7684...88d58f )
by BruceScrutinizer
02:01
created

Config   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 164
rs 10
c 0
b 0
f 0
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathComposerJson() 0 3 1
A getPlotter() 0 3 1
A getMode() 0 3 1
A __construct() 0 18 1
A getVersion() 0 4 1
A cloneWithWithOverride() 0 16 1
A print() 0 20 2
A getPublicEntries() 0 3 1
A getStartPath() 0 3 1
A getPrivateEntries() 0 3 1
A validateLoadedConfig() 0 8 1
A enabledCache() 0 3 1
A populateOutputVarFromArray() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace NamespaceProtector\Config;
4
5
use NamespaceProtector\Common\PathInterface;
6
use Webmozart\Assert\Assert;
7
8
final class Config
9
{
10
    public const MODE_MAKE_VENDOR_PRIVATE = 'MODE_MAKE_VENDOR_PRIVATE';
11
    public const MODE_PUBLIC = 'PUBLIC';
12
13
    public const PLOTTER_TERMINAL = 'plotter-terminal';
14
    public const PLOTTER_PNG = 'plotter-png';
15
16
    /** @var PathInterface */
17
    private $pathStart;
18
19
    /** @var PathInterface  */
20
    private $pathComposerJson;
21
22
    /** @var array<string> */
23
    private $privateEntries;
24
25
    /** @var array<string> */
26
    private $publicEntries;
27
28
    /** @var string */
29
    private $mode;
30
31
    /** @var string */
32
    private $version;
33
34
    /** @var bool */
35
    private $enabledCache;
36
37
    /** @var string */
38
    private $plotter;
39
40
    /**
41
     * @param array<string> $privateEntries
42
     * @param array<string> $publicEntries
43
     */
44
    public function __construct(
45
        string $version,
46
        PathInterface $pathStart,
47
        PathInterface $pathComposerJson,
48
        array $privateEntries,
49
        array $publicEntries,
50
        string $mode = self::MODE_PUBLIC,
51
        bool $enabledCache = false,
52
        string $plotter = self::PLOTTER_TERMINAL
53
    ) {
54
        $this->version = $version;
55
        $this->pathStart = $pathStart;
56
        $this->pathComposerJson = $pathComposerJson;
57
        $this->privateEntries = $privateEntries;
58
        $this->publicEntries = $publicEntries;
59
        $this->mode = $mode;
60
        $this->enabledCache = $enabledCache;
61
        $this->plotter = $plotter;
62
    }
63
64
    public function getStartPath(): PathInterface
65
    {
66
        return $this->pathStart;
67
    }
68
69
    /**
70
     * @return array<string>
71
     */
72
    public function getPrivateEntries(): array
73
    {
74
        return $this->privateEntries;
75
    }
76
77
    /**
78
     * @return array<string>
79
     */
80
    public function getPublicEntries(): array
81
    {
82
        return $this->publicEntries;
83
    }
84
85
    public function getMode(): string
86
    {
87
        return $this->mode;
88
    }
89
90
    public function getPlotter(): string
91
    {
92
        return $this->plotter;
93
    }
94
95
    public function getPathComposerJson(): PathInterface
96
    {
97
        return $this->pathComposerJson;
98
    }
99
100
    public function print(): string
101
    {
102
        //todo: automatic dump config
103
104
        $prettyPrintPrivateEntries = $this->populateOutputVarFromArray($this->getPrivateEntries());
105
        $prettyPrintPublicEntries = $this->populateOutputVarFromArray($this->getPublicEntries());
106
107
        return
108
            '' . PHP_EOL .
109
            '|Dump config:' . PHP_EOL .
110
            '|> Version: ' . $this->getVersion() . PHP_EOL .
111
            '|> Cache: ' . ($this->enabledCache() === true ? 'TRUE' : 'FALSE') . PHP_EOL .
112
            '|> Plotter: ' . $this->getPlotter() . PHP_EOL .
113
            '|> Path start: ' . $this->pathStart->get() . PHP_EOL .
114
            '|> Composer Json path: ' . $this->pathComposerJson->get() . PHP_EOL .
115
            '|> Mode: ' . $this->getMode() . PHP_EOL .
116
            '|> Private entries: ' . $prettyPrintPrivateEntries . PHP_EOL .
117
            '|' . PHP_EOL .
118
            '|> Public entries: ' . $prettyPrintPublicEntries . PHP_EOL .
119
            '';
120
    }
121
122
    /**
123
     * @param array<string> $entries
124
     */
125
    private function populateOutputVarFromArray(array $entries): string
126
    {
127
        $prettyPrintNamespaceToValidate = "\n";
128
        foreach ($entries as $namespace) {
129
            $prettyPrintNamespaceToValidate .= '|       >' . $namespace . \PHP_EOL;
130
        }
131
        return $prettyPrintNamespaceToValidate;
132
    }
133
134
    /** @param array<string,string> $parameters */
135
    public function cloneWithWithOverride(self $config, array $parameters): self
136
    {
137
        $self = new self(
138
            $config->getVersion(),
139
            $config->getStartPath(),
140
            $config->getPathComposerJson(),
141
            $config->getPrivateEntries(),
142
            $config->getPublicEntries(),
143
            $config->getMode(),
144
            $config->enabledCache(),
145
            $parameters['plotter'] ?? $config->getPlotter(),
146
        );
147
148
        $self->validateLoadedConfig(); //todo: validation called multiple times
149
150
        return $self;
151
    }
152
153
    public function validateLoadedConfig(): void
154
    {
155
        Assert::inArray($this->getMode(), [self::MODE_PUBLIC, self::MODE_MAKE_VENDOR_PRIVATE], 'Mode not valid');
156
        Assert::eq('0.1.0', $this->getVersion(), 'Version not valid');
157
        Assert::directory($this->getStartPath()->get(), 'Start directory not valid');
158
        Assert::directory($this->getPathComposerJson()->get(), 'Composer json directory not valid');
159
        Assert::boolean($this->enabledCache(), 'Cache flag must be boolean');
160
        Assert::inArray($this->getPlotter(), [self::PLOTTER_TERMINAL, self::PLOTTER_PNG], 'Plotter not valid');
161
    }
162
163
    public function getVersion(): string
164
    {
165
        //todo: use https://github.com/nikolaposa/version
166
        return $this->version;
167
    }
168
169
    public function enabledCache(): bool
170
    {
171
        return $this->enabledCache;
172
    }
173
}
174