Passed
Pull Request — master (#407)
by Kirill
05:29
created

Uri::setPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Storage\Parser;
13
14
use Spiral\Storage\Exception\UriException;
15
16
final class Uri implements UriInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private const SCHEME_PATH_DELIMITER = '://';
22
23
    /**
24
     * @var string
25
     */
26
    private const PATH_DELIMITER = '/';
27
28
    /**
29
     * @var string
30
     */
31
    private $fs;
32
33
    /**
34
     * @var string
35
     */
36
    private $path;
37
38
    /**
39
     * @param string $fs
40
     * @param string $path
41
     * @throws UriException
42
     */
43
    public function __construct(string $fs, string $path)
44
    {
45
        $this->setFileSystem($fs);
46
        $this->setPath($path);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function __toString(): string
53
    {
54
        return $this->getFileSystem() . self::SCHEME_PATH_DELIMITER . $this->getPath();
55
    }
56
57
    /**
58
     * @param string $fs
59
     * @param string $path
60
     * @return static
61
     * @throws UriException
62
     */
63
    public static function create(string $fs, string $path): self
64
    {
65
        return new self($fs, $path);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function withFileSystem(string $fs): UriInterface
72
    {
73
        $self = clone $this;
74
        $self->setFileSystem($fs);
75
76
        return $self;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function withPath(string $path): UriInterface
83
    {
84
        $self = clone $this;
85
        $self->setPath($path);
86
87
        return $self;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function getFileSystem(): string
94
    {
95
        return $this->fs;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function getPath(): string
102
    {
103
        return $this->path;
104
    }
105
106
    /**
107
     * @param string $fs
108
     * @throws UriException
109
     */
110
    private function setFileSystem(string $fs): void
111
    {
112
        if ($fs === '') {
113
            throw new UriException('Filesystem name can not be empty');
114
        }
115
116
        $this->fs = $fs;
117
    }
118
119
    /**
120
     * @param string $path
121
     * @throws UriException
122
     */
123
    private function setPath(string $path): void
124
    {
125
        $path = \str_replace(['\\', '/'], self::PATH_DELIMITER, $path);
126
        $path = \trim(\trim($path), self::PATH_DELIMITER);
127
128
        if ($path === '') {
129
            throw new UriException('Filesystem pathname can not be empty');
130
        }
131
132
        $this->path = $path;
133
    }
134
}
135