Passed
Pull Request — master (#5)
by Enjoys
11:24
created

Environment::setMinifyJS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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