Completed
Push — default-schema-host-name ( b35358 )
by Akihito
03:02
created

UriFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 36
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 8 2
A validate() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Annotation\ContextSchema;
8
use BEAR\Resource\Exception\UriException;
9
use function parse_url;
10
11
final class UriFactory
12
{
13
    /**
14
     * @var string
15
     */
16
    private $schemaHost;
17
18
    /**
19
     * @ContextSchema
20
     */
21
    public function __construct(string $schemaHost = 'page://self')
22
    {
23
        $this->schemaHost = $schemaHost;
24
    }
25
26
    public function __invoke($uri, array $query = [])
27
    {
28
        if (array_keys(parse_url($uri)) === ['path']) {
29
            $uri = $this->schemaHost . $uri;
30
        }
31
32
        return new Uri($uri, $query);
33
    }
34
35
    /**
36
     * @throws UriException
37
     */
38
    private function validate(string $uri)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
39
    {
40
        if (! filter_var($uri, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
41
            $msg = is_string($uri) ? $uri : gettype($uri);
42
43
            throw new UriException($msg, 500);
44
        }
45
    }
46
}
47