Passed
Push — main ( 8bfeba...683260 )
by Teodoro
01:57
created

Bucket::version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tleckie\Assets;
6
7
use Tleckie\Assets\Versioned\VersionedInterface;
8
use function rtrim;
9
10
/**
11
 * Class Bucket
12
 *
13
 * @package  Tleckie\Assets
14
 * @category Bucket
15
 * @author   Teodoro Leckie Westberg <[email protected]>
16
 */
17
class Bucket implements BucketInterface
18
{
19
    /**
20
     * @var VersionedInterface
21
     */
22
    protected VersionedInterface $version;
23
24
    /**
25
     * @var string
26
     */
27
    protected string $path;
28
29
    /**
30
     * Bucket constructor.
31
     *
32
     * @param VersionedInterface $version
33
     * @param string             $path
34
     */
35
    public function __construct(VersionedInterface $version, string $path = '')
36
    {
37
        $this->version = $version;
38
        $this->path = rtrim($path, '/');
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function url(string $asset): string
45
    {
46
        $versioned = ltrim($this->version->applyVersion($asset), '/');
47
48
        if ($this->hasScheme($this->path())) {
49
            return sprintf("%s/%s", $this->path(), $versioned);
50
        }
51
52
        if ($this->isAbsolute($asset)) {
53
            return sprintf('/%s', $versioned);
54
        }
55
56
        if (empty($this->path)) {
57
            return $versioned;
58
        }
59
60
        return sprintf("%s/%s", $this->path(), $versioned);
61
    }
62
63
    /**
64
     * @param string $path
65
     * @return bool
66
     */
67
    private function hasScheme(string $path): bool
68
    {
69
        return str_contains($path, '://') || str_starts_with($path, '//');
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function path(): ?string
76
    {
77
        return $this->path;
78
    }
79
80
    /**
81
     * @param string $asset
82
     * @return bool
83
     */
84
    private function isAbsolute(string $asset)
85
    {
86
        return str_starts_with($asset, '/');
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function version(): string
93
    {
94
        return $this->version->version();
95
    }
96
}
97