Passed
Push — master ( 4b0844...7a0735 )
by David
03:12
created

DsnValue::getFragment()   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 DavidGarcia\ValueObject\Primitive\StringAlternatives;
6
7
use DavidGarcia\ValueObject\Exception\InvalidValueException;
8
use DavidGarcia\ValueObject\Primitive\AbstractValue;
9
use InvalidArgumentException;
10
use Webmozart\Assert\Assert;
11
12
class DsnValue extends AbstractValue
13
{
14
    private string $value;
15
    private ?string $scheme;
16
    private ?string $user;
17
    private ?string $pass;
18
    private ?string $host;
19
    private ?int $port;
20
    private ?string $path;
21
    private array $query = [];
22
    private ?string $fragment;
23
24 16
    private function __construct(string $value, bool $cache)
25
    {
26 16
        $this->value = $value;
27
28
        // NULL or String
29
30 16
        $this->scheme = parse_url($this->value, PHP_URL_SCHEME);
31 16
        $this->user = parse_url($this->value, PHP_URL_USER);
32 16
        $this->pass = parse_url($this->value, PHP_URL_PASS);
33 16
        $this->host = parse_url($this->value, PHP_URL_HOST);
34 16
        $this->path = parse_url($this->value, PHP_URL_PATH);
35 16
        $this->fragment = parse_url($this->value, PHP_URL_FRAGMENT);
36
37
        // NULL or Int
38
39 16
        $this->port = parse_url($this->value, PHP_URL_PORT);
40
41
        // NULL or Array
42
43 16
        parse_str((string) parse_url($this->value, PHP_URL_QUERY), $this->query);
44
45
        // Validation
46
47
        try {
48 16
            Assert::stringNotEmpty($this->scheme, 'Unable to extract the DSN Scheme');
49 16
            Assert::stringNotEmpty($this->host, 'Unable to extract the DSN Host');
50 1
        } catch (InvalidArgumentException $exception) {
51 1
            throw new InvalidValueException($exception->getMessage(), $exception);
52
        }
53
54 16
        if ($cache) {
55 1
            self::cacheStore($this->value, $this);
56
        }
57 16
    }
58
59 16
    public static function create(string $value, bool $cache = false): DsnValue
60
    {
61 16
        $trimmed = trim($value);
62
63
        try {
64 16
            Assert::stringNotEmpty($trimmed, 'Data Source Name (DSN) value cannot be empty');
65 1
        } catch (InvalidArgumentException $exception) {
66 1
            throw new InvalidValueException($exception->getMessage(), $exception);
67
        }
68
69 16
        $object = $cache ? parent::cacheRetrieve($trimmed) : null;
70
71 16
        if (null === $object) {
72 16
            $object = new self($trimmed, $cache);
73
        }
74
75 16
        return $object;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 1
    public function getValue(): string
82
    {
83 1
        return $this->value;
84
    }
85
86
    /**
87
     * @return string
88
     */
89 1
    public function getSanitizedValue(): string
90
    {
91 1
        return sprintf(
92 1
            '%s://%s%s%s%s%s',
93 1
            $this->scheme,
94 1
            $this->host,
95 1
            (!empty($this->port) ? sprintf(':%d', $this->port) : ''),
96 1
            $this->path,
97 1
            (!empty($this->query) ? sprintf('?%s', http_build_query($this->query)) : ''),
98 1
            (!empty($this->query) ? sprintf('#%s', $this->fragment) : '')
99
        );
100
    }
101
102
    /**
103
     * @return string
104
     */
105 1
    public function getScheme(): string
106
    {
107 1
        return $this->scheme;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->scheme could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
108
    }
109
110
    /**
111
     * @return null|string
112
     */
113 1
    public function getUser(): ?string
114
    {
115 1
        return $this->user;
116
    }
117
118
    /**
119
     * @return null|string
120
     */
121 1
    public function getPass(): ?string
122
    {
123 1
        return $this->pass;
124
    }
125
126
    /**
127
     * @return null|string
128
     */
129 2
    public function getAuthorisationBasicToken(): ?string
130
    {
131 2
        if (null === $this->user && null === $this->pass) {
132 1
            return null;
133
        }
134
135 1
        return base64_encode(sprintf('%s:%s', $this->user, $this->pass));
136
    }
137
138
    /**
139
     * @return string
140
     */
141 1
    public function getHost(): string
142
    {
143 1
        return $this->host;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->host could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
144
    }
145
146
    /**
147
     * @return null|int
148
     */
149 2
    public function getPort(): ?int
150
    {
151 2
        return $this->port;
152
    }
153
154
    /**
155
     * @return null|string
156
     */
157 1
    public function getPath(): ?string
158
    {
159 1
        return $this->path;
160
    }
161
162
    /**
163
     * @return null|array
164
     */
165 1
    public function getQuery(): ?array
166
    {
167 1
        return !empty($this->query) ? $this->query : null;
168
    }
169
170
    /**
171
     * @return null|string
172
     */
173 1
    public function getFragment(): ?string
174
    {
175 1
        return $this->fragment;
176
    }
177
}
178