Passed
Push — master ( a8959a...ad2bf9 )
by Enjoys
01:55
created

Environment   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 91.38%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 49
c 2
b 0
f 1
dl 0
loc 200
ccs 53
cts 58
cp 0.9138
rs 10
wmc 22

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setVersion() 0 4 1
A setBaseUrl() 0 4 1
A setCacheTime() 0 4 1
A setParamVersion() 0 4 1
A getBaseUrl() 0 3 1
A getVersion() 0 6 2
A getCacheTime() 0 3 1
A getCompileDir() 0 3 1
A setStrategy() 0 4 1
A setLogger() 0 3 1
A getStrategy() 0 3 1
A getRender() 0 3 1
A setJsMinifyOptions() 0 3 1
A getLogger() 0 3 1
A setCssMinifyOptions() 0 3 1
A getMinifyOptions() 0 3 1
A __construct() 0 14 2
A setCompileDir() 0 7 2
A getProjectDir() 0 3 1
1
<?php
2
3
namespace Enjoys\AssetsCollector;
4
5
use Enjoys\AssetsCollector\Exception\PathDirectoryIsNotValid;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
9
class Environment
10
{
11
12
    private string $projectDir;
13
    private string $compileDir;
14
    private string $baseUrl = '';
15
16
    private int $cacheTime = -1;
17
18
    private int $strategy = Assets::STRATEGY_MANY_FILES;
19
    private string $render = Assets::RENDER_HTML;
20
21
    private ?string $version = null;
22
    private string $paramVersion = '?v=';
23
24
    /**
25
     * @var array{css: array, js: array}
26
     */
27
    private array $minifyOptions = [
28
        'css' => [],
29
        'js' => []
30
    ];
31
32
    private LoggerInterface $logger;
33
34
    /**
35
     * Environment constructor.
36
     * @param string $compileDir
37
     * @param string $projectDir Если пустая строка, то realpath вернет текущую рабочую директорию
38
     * @noinspection PhpUnnecessaryCurlyVarSyntaxInspection
39
     */
40 61
    public function __construct(string $compileDir = '/', string $projectDir = '')
41
    {
42 61
        $projectDir = realpath($projectDir);
43
44 61
        if ($projectDir === false) {
45 1
            throw new PathDirectoryIsNotValid(
46 1
                "Не установлена директория проекта или не удалось автоматически определить директорию"
47
            );
48
        }
49 60
        $this->projectDir = $projectDir;
50 60
        \putenv("ASSETS_PROJECT_DIRECTORY={$this->projectDir}/");
51
52 60
        $this->compileDir = $this->setCompileDir($compileDir);
53 60
        $this->logger = new NullLogger();
54 60
    }
55
56
    /**
57
     * @return string
58
     */
59 60
    public function getProjectDir(): string
60
    {
61 60
        return $this->projectDir;
62
    }
63
64
65
    /**
66
     * Функцию realpath() нельзя применять так как директории изначально может не быть,
67
     * она может потом быть создана, если будут права
68
     * @param string $path
69
     * @return string
70
     */
71 60
    private function setCompileDir(string $path): string
72
    {
73 60
        if (str_starts_with($path, $this->getProjectDir())) {
74 25
            $path = str_replace($this->getProjectDir(), '', $path);
75
        }
76 60
        $path = $this->getProjectDir() . '/' . ltrim($path, '/\.');
77 60
        return rtrim($path, '/');
78
    }
79
80
    /**
81
     * @return string
82
     */
83 21
    public function getCompileDir(): string
84
    {
85 21
        return $this->compileDir;
86
    }
87
88
89
    /**
90
     * @param string $baseUrl
91
     * @return $this
92
     */
93 34
    public function setBaseUrl(string $baseUrl = '/'): Environment
94
    {
95 34
        $this->baseUrl = rtrim($baseUrl, '\/');
96 34
        return $this;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 16
    public function getBaseUrl(): string
103
    {
104 16
        return $this->baseUrl;
105
    }
106
107
    /**
108
     * @param string $version
109
     * @return $this
110
     */
111 1
    public function setVersion(string $version): Environment
112
    {
113 1
        $this->version = $version;
114 1
        return $this;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 9
    public function getVersion(): ?string
121
    {
122 9
        if ($this->version === null) {
123 9
            return null;
124
        }
125 1
        return $this->paramVersion . $this->version;
126
    }
127
128
    /**
129
     * @param string $paramVersion
130
     * @return $this
131
     */
132 1
    public function setParamVersion(string $paramVersion): Environment
133
    {
134 1
        $this->paramVersion = $paramVersion;
135 1
        return $this;
136
    }
137
138
139
    /**
140
     * @param int $cacheTime
141
     * @return $this
142
     */
143 1
    public function setCacheTime(int $cacheTime): Environment
144
    {
145 1
        $this->cacheTime = $cacheTime;
146 1
        return $this;
147
    }
148
149
    /**
150
     * @return int
151
     */
152 3
    public function getCacheTime(): int
153
    {
154 3
        return $this->cacheTime;
155
    }
156
157 7
    public function getStrategy(): int
158
    {
159 7
        return $this->strategy;
160
    }
161
162 1
    public function getRender(): string
163
    {
164 1
        return $this->render;
165
    }
166
167
    /**
168
     * @param int $strategy
169
     * @return Environment
170
     */
171 11
    public function setStrategy(int $strategy): Environment
172
    {
173 11
        $this->strategy = $strategy;
174 11
        return $this;
175
    }
176
177 14
    public function getLogger(): LoggerInterface
178
    {
179 14
        return $this->logger;
180
    }
181
182 1
    public function setLogger(LoggerInterface $logger): void
183
    {
184 1
        $this->logger = $logger;
185 1
    }
186
187
    /**
188
     * @return array{css: array, js: array}
189
     */
190 2
    public function getMinifyOptions(): array
191
    {
192 2
        return $this->minifyOptions;
193
    }
194
195
    /**
196
     * @param array|array[] $options
197
     */
198
    public function setCssMinifyOptions(array $options): void
199
    {
200
        $this->minifyOptions['css'] = $options;
201
    }
202
203
    /**
204
     * @param array|array[] $options
205
     */
206
    public function setJsMinifyOptions(array $options): void
207
    {
208
        $this->minifyOptions['js'] = $options;
209
    }
210
}
211