Passed
Pull Request — master (#407)
by Kirill
09:14 queued 02:20
created

StaticResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
dl 0
loc 62
rs 10
c 1
b 0
f 1
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 3 1
A resolve() 0 4 1
A fromFactory() 0 3 1
A suffix() 0 7 1
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\Distribution\Resolver;
13
14
use GuzzleHttp\Psr7\Uri;
15
use Psr\Http\Message\UriFactoryInterface;
16
use Psr\Http\Message\UriInterface;
17
18
class StaticResolver extends Resolver
19
{
20
    /**
21
     * @var string
22
     */
23
    private const URI_PATH_DELIMITER = '/';
24
25
    /**
26
     * @var UriInterface
27
     */
28
    private $host;
29
30
    /**
31
     * @param UriInterface $host
32
     */
33
    public function __construct(UriInterface $host)
34
    {
35
        $this->host = clone $host;
36
    }
37
38
    /**
39
     * @param UriFactoryInterface $factory
40
     * @param string $host
41
     * @return static
42
     */
43
    public static function fromFactory(UriFactoryInterface $factory, string $host): self
44
    {
45
        return new static($factory->createUri($host));
46
    }
47
48
    /**
49
     * @param string $host
50
     * @return static
51
     */
52
    public static function create(string $host): self
53
    {
54
        return new static(new Uri($host));
55
    }
56
57
    /**
58
     * @param string $file
59
     * @param array<string, string> $query
60
     * @return UriInterface
61
     */
62
    public function resolve(string $file, array $query = []): UriInterface
63
    {
64
        return $this->host->withPath($this->suffix($file))
65
            ->withQuery(\http_build_query($query))
66
        ;
67
    }
68
69
    /**
70
     * @param string $file
71
     * @return string
72
     */
73
    private function suffix(string $file): string
74
    {
75
        $prefix = $this->host->getPath();
76
77
        return \implode(self::URI_PATH_DELIMITER, [
78
            \trim($prefix, self::URI_PATH_DELIMITER),
79
            \trim($file, self::URI_PATH_DELIMITER),
80
        ]);
81
    }
82
}
83