Passed
Push — master ( 4caa02...6ed31c )
by Enjoys
56s queued 12s
created

Asset::isReplaceRelativeUrls()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
17
    private ?string $id = null;
18
19
    /**
20
     * @var string|false
21
     */
22
    private $path;
23
    private string $type;
24
    private bool $isUrl;
25
    private string $origPath;
26
    private bool $minify;
27
    private bool $replaceRelativeUrls;
28
    private string $url = '';
29
30
31 41
    public function __construct(string $type, string $path, array $params = [])
32
    {
33 41
        $this->setOptions($params);
34 41
        $this->type = $type;
35 41
        $this->origPath = $path;
36 41
        $this->minify = (bool)$this->getOption(self::MINIFY, true);
37 41
        $this->replaceRelativeUrls = (bool)$this->getOption(self::REPLACE_RELATIVE_URLS, true);
38 41
        $this->isUrl = $this->checkIsUrl($path);
39 41
        $this->path = $this->getNormalizedPath($path);
40 41
    }
41
42
    /**
43
     * @param string $path
44
     * @return false|string
45
     */
46 41
    private function getNormalizedPath(string $path)
47
    {
48 41
        if ($this->isUrl()) {
49 21
            $this->setId($this->url);
50 21
            return $this->url;
51
        }
52
53 23
        if (false === $projectDir = \getenv('ASSETS_PROJECT_DIRECTORY')) {
54
            $projectDir = '';
55
        }
56
        $paths = [
57 23
            $path,
58 23
            $projectDir . $path
59
        ];
60
61 23
        foreach ($paths as $path) {
62 23
            if (false !== $normalizedPath = realpath($path)) {
63 20
                $this->setId($normalizedPath);
64 20
                break;
65
            }
66
        }
67 23
        return $normalizedPath;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $normalizedPath seems to be defined by a foreach iteration on line 61. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
68
    }
69
70 41
    private function checkIsUrl(string $path): bool
71
    {
72 41
        if (\str_starts_with($path, '//')) {
73 15
            $this->url = Helpers::getHttpScheme() . ':' . $path;
74 15
            return true;
75
        }
76
77 28
        if (in_array(strpos($path, '://'), [4, 5])) {
78 7
            $this->url = $path;
79 7
            return true;
80
        }
81 23
        return false;
82
    }
83
84
85
86 12
    public function isMinify(): bool
87
    {
88 12
        return $this->minify;
89
    }
90
91
    /**
92
     * @return false|string
93
     */
94 31
    public function getPath()
95
    {
96 31
        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...
97
    }
98
99 20
    public function getType(): string
100
    {
101 20
        return $this->type;
102
    }
103
104 41
    public function isUrl(): bool
105
    {
106 41
        return $this->isUrl;
107
    }
108
109
110
    /**
111
     * @return string|null
112
     */
113 18
    public function getId(): ?string
114
    {
115 18
        return $this->id;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 4
    public function getOrigPath(): string
122
    {
123 4
        return $this->origPath;
124
    }
125
126 38
    private function setId(string $path): void
127
    {
128 38
        $this->id = md5($path);
129 38
    }
130
131
    /**
132
     * @return bool
133
     */
134 5
    public function isReplaceRelativeUrls(): bool
135
    {
136 5
        return $this->replaceRelativeUrls;
137
    }
138
}
139