Passed
Push — master ( bf8b2f...effdba )
by Enjoys
01:33
created

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