Passed
Branch master (c2c9e6)
by Enjoys
02:24
created

Asset::isNotCollect()   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
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector;
6
7
use Enjoys\Traits\Options;
8
9
class Asset
10
{
11
    use Options;
12
13
    public const MINIFY = 'minify';
14
    public const REPLACE_RELATIVE_URLS = 'reaplaceRelativeUrls';
15
    public const CREATE_SYMLINK = 'symlinks';
16
    public const NOT_COLLECT = 'notCollect';
17
    public const ATTRIBUTES = 'attributes';
18
19
    private ?string $id = null;
20
21
    /**
22
     * @var string|false
23
     */
24
    private $path;
25
    private string $type;
26
    private bool $isUrl;
27
    private string $origPath;
28
    private bool $minify;
29
    private bool $replaceRelativeUrls;
30
    private string $url = '';
31
    private bool $notCollect;
32
    /**
33
     * @var array<string, string|null>|null
34
     */
35
    private ?array $attributes = null;
36
37
38
    /**
39
     * @psalm-suppress MixedAssignment
40
     */
41 46
    public function __construct(string $type, string $path, array $params = [])
42
    {
43 46
        $this->setOptions($params);
44 46
        $this->type = $type;
45 46
        $this->origPath = $path;
46 46
        $this->minify = (bool)$this->getOption(self::MINIFY, true);
47 46
        $this->replaceRelativeUrls = (bool)$this->getOption(self::REPLACE_RELATIVE_URLS, true);
48 46
        $this->notCollect = (bool)$this->getOption(self::NOT_COLLECT, false);
49 46
        $this->attributes = $this->getOption(self::ATTRIBUTES, null, false);
50 46
        $this->isUrl = $this->checkIsUrl($path);
51 46
        $this->path = $this->getNormalizedPath($path);
52
53
    }
54
55
    /**
56
     * @param string $path
57
     * @return false|string
58
     */
59 46
    private function getNormalizedPath(string $path)
60
    {
61 46
        if ($this->isUrl()) {
62 25
            $this->setId($this->url);
63 25
            return $this->url;
64
        }
65
66 24
        if (false === $projectDir = \getenv('ASSETS_PROJECT_DIRECTORY')) {
67
            $projectDir = '';
68
        }
69
        $paths = [
70
            $path,
71 24
            $projectDir . $path
72
        ];
73
74 24
        foreach ($paths as $path) {
75 24
            if (false !== $normalizedPath = realpath($path)) {
76 21
                $this->setId($normalizedPath);
77 21
                break;
78
            }
79
        }
80 24
        return $normalizedPath;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $normalizedPath seems to be defined by a foreach iteration on line 74. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
81
    }
82
83 46
    private function checkIsUrl(string $path): bool
84
    {
85 46
        if (\str_starts_with($path, '//')) {
86 16
            $this->url = Helpers::getHttpScheme() . ':' . $path;
87 16
            return true;
88
        }
89
90 32
        if (in_array(strpos($path, '://'), [4, 5])) {
91 7
            $this->url = $path;
92 7
            return true;
93
        }
94
95 27
        if (\str_starts_with($path, 'url:') || \str_starts_with($path, 'local:')) {
96 3
            $this->url = str_replace(['url:', 'local:'], '', $path);
97 3
            return true;
98
        }
99
100 24
        return false;
101
    }
102
103
104 13
    public function isMinify(): bool
105
    {
106 13
        return $this->minify;
107
    }
108
109
    /**
110
     * @return false|string
111
     */
112 36
    public function getPath()
113
    {
114 36
        return $this->path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->path also could return the type boolean which is incompatible with the documented return type false|string.
Loading history...
115
    }
116
117 23
    public function getType(): string
118
    {
119 23
        return $this->type;
120
    }
121
122 46
    public function isUrl(): bool
123
    {
124 46
        return $this->isUrl;
125
    }
126
127 20
    public function getId(): ?string
128
    {
129 20
        return $this->id;
130
    }
131
132 4
    public function getOrigPath(): string
133
    {
134 4
        return $this->origPath;
135
    }
136
137 43
    private function setId(string $path): void
138
    {
139 43
        $this->id = md5($path);
140
    }
141
142 6
    public function isReplaceRelativeUrls(): bool
143
    {
144 6
        return $this->replaceRelativeUrls;
145
    }
146
147 3
    public function isNotCollect(): bool
148
    {
149 3
        return $this->notCollect;
150
    }
151
152
    /**
153
     * @return array<string, string|null>|null
154
     */
155 11
    public function getAttributes(): ?array
156
    {
157 11
        return $this->attributes;
158
    }
159
}
160