Url::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Domain\ValueObject;
4
5
final class Url
6
{
7
    /** @var string */
8
    private $value;
9
10 11
    public function __construct(string $value)
11
    {
12 11
        if (!$this->validateUrl($value)) {
13 1
            throw new \InvalidArgumentException(sprintf('URL "%s" is not supported', $value));
14
        }
15
16 10
        $this->value = $value;
17 10
    }
18
19 11
    private function validateUrl(string $url): bool
20
    {
21
        // Validate SSH URLs
22 11
        if (preg_match('/^git@[-a-z0-9\.]+:[\w-]+\/[\w-]+\.git$/', $url)) {
23 7
            return true;
24
        }
25
26
        // Validate HTTP URLs
27 6
        if (filter_var($url, FILTER_VALIDATE_URL)) {
28 5
            return true;
29
        }
30
31 1
        return false;
32
    }
33
34
    public function getValue(): string
35
    {
36
        return $this->value;
37
    }
38
39 8
    public function __toString()
40
    {
41 8
        return $this->value;
42
    }
43
}
44