Passed
Push — dev ( 902aab...52b0ab )
by Enjoys
03:17 queued 58s
created

Asset   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 97.87%

Importance

Changes 6
Bugs 1 Features 3
Metric Value
eloc 51
c 6
b 1
f 3
dl 0
loc 134
ccs 46
cts 47
cp 0.9787
rs 10
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getNormalizedPath() 0 22 5
A getPath() 0 3 1
A setId() 0 3 1
A getOrigPath() 0 3 1
A getType() 0 3 1
A checkIsUrl() 0 18 5
A isReplaceRelativeUrls() 0 3 1
A isUrl() 0 3 1
A isMinify() 0 3 1
A getId() 0 3 1
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 43
    public function __construct(string $type, string $path, array $params = [])
32
    {
33 43
        $this->setOptions($params);
34 43
        $this->type = $type;
35 43
        $this->origPath = $path;
36 43
        $this->minify = (bool)$this->getOption(self::MINIFY, true);
37 43
        $this->replaceRelativeUrls = (bool)$this->getOption(self::REPLACE_RELATIVE_URLS, true);
38 43
        $this->isUrl = $this->checkIsUrl($path);
39 43
        $this->path = $this->getNormalizedPath($path);
40
    }
41
42
    /**
43
     * @param string $path
44
     * @return false|string
45
     */
46 43
    private function getNormalizedPath(string $path)
47
    {
48 43
        if ($this->isUrl()) {
49 23
            $this->setId($this->url);
50 23
            return $this->url;
51
        }
52
53 23
        if (false === $projectDir = \getenv('ASSETS_PROJECT_DIRECTORY')) {
54
            $projectDir = '';
55
        }
56
        $paths = [
57
            $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 43
    private function checkIsUrl(string $path): bool
71
    {
72 43
        if (\str_starts_with($path, '//')) {
73 15
            $this->url = Helpers::getHttpScheme() . ':' . $path;
74 15
            return true;
75
        }
76
77 30
        if (in_array(strpos($path, '://'), [4, 5])) {
78 7
            $this->url = $path;
79 7
            return true;
80
        }
81
82 25
        if (\str_starts_with($path, 'url:') || \str_starts_with($path, 'local:')) {
83 2
            $this->url = str_replace(['url:', 'local:'], '', $path);
84 2
            return true;
85
        }
86
87 23
        return false;
88
    }
89
90
91
92 12
    public function isMinify(): bool
93
    {
94 12
        return $this->minify;
95
    }
96
97
    /**
98
     * @return false|string
99
     */
100 33
    public function getPath()
101
    {
102 33
        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...
103
    }
104
105 20
    public function getType(): string
106
    {
107 20
        return $this->type;
108
    }
109
110 43
    public function isUrl(): bool
111
    {
112 43
        return $this->isUrl;
113
    }
114
115
116
    /**
117
     * @return string|null
118
     */
119 18
    public function getId(): ?string
120
    {
121 18
        return $this->id;
122
    }
123
124
    /**
125
     * @return string
126
     */
127 4
    public function getOrigPath(): string
128
    {
129 4
        return $this->origPath;
130
    }
131
132 40
    private function setId(string $path): void
133
    {
134 40
        $this->id = md5($path);
135
    }
136
137
    /**
138
     * @return bool
139
     */
140 5
    public function isReplaceRelativeUrls(): bool
141
    {
142 5
        return $this->replaceRelativeUrls;
143
    }
144
}
145