Passed
Push — master ( 59589b...c79df9 )
by Enjoys
05:41 queued 03:11
created

Environment::getRequestFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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