Passed
Push — master ( 3436a5...44e82b )
by Akmal
01:09
created

Uri::getAuthority()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace OpenEngine\Mika\Core\Components\Http\Message\Uri;
4
5
use OpenEngine\Mika\Core\Components\Http\Message\Uri\Traits\CloneableHostTrait;
6
use OpenEngine\Mika\Core\Components\Http\Message\Uri\Traits\CloneableQueryTrait;
7
use Psr\Http\Message\UriInterface;
8
9
class Uri implements UriInterface
10
{
11
    use CloneableHostTrait;
12
    use CloneableQueryTrait;
13
14
    /**
15
     * Uri constructor.
16
     * @param null|string $uri
17
     */
18
    public function __construct(?string $uri = null)
19
    {
20
        if ($uri === null) {
21
            return;
22
        }
23
24
        $this
25
            ->setScheme(parse_url($uri, PHP_URL_SCHEME))
26
            ->setUser(parse_url($uri, PHP_URL_USER))
27
            ->setPassword(parse_url($uri, PHP_URL_PASS))
28
            ->setHost(parse_url($uri, PHP_URL_HOST))
29
            ->setPort(parse_url($uri, PHP_URL_PORT))
0 ignored issues
show
Bug introduced by
parse_url($uri, OpenEngi...ssage\Uri\PHP_URL_PORT) of type string is incompatible with the type null|integer expected by parameter $port of OpenEngine\Mika\Core\Com...sage\Uri\Uri::setPort(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
            ->setPort(/** @scrutinizer ignore-type */ parse_url($uri, PHP_URL_PORT))
Loading history...
30
            ->setPath(parse_url($uri, PHP_URL_PATH))
31
            ->setQuery(parse_url($uri, PHP_URL_QUERY))
32
            ->setFragment(parse_url($uri, PHP_URL_FRAGMENT));
33
    }
34
35
    /**
36
     * Return the string representation as a URI reference.
37
     *
38
     * Depending on which components of the URI are present, the resulting
39
     * string is either a full URI or relative reference according to RFC 3986,
40
     * Section 4.1. The method concatenates the various components of the URI,
41
     * using the appropriate delimiters:
42
     *
43
     * - If a scheme is present, it MUST be suffixed by ":".
44
     * - If an authority is present, it MUST be prefixed by "//".
45
     * - The path can be concatenated without delimiters. But there are two
46
     *   cases where the path has to be adjusted to make the URI reference
47
     *   valid as PHP does not allow to throw an exception in __toString():
48
     *     - If the path is rootless and an authority is present, the path MUST
49
     *       be prefixed by "/".
50
     *     - If the path is starting with more than one "/" and no authority is
51
     *       present, the starting slashes MUST be reduced to one.
52
     * - If a query is present, it MUST be prefixed by "?".
53
     * - If a fragment is present, it MUST be prefixed by "#".
54
     *
55
     * @see http://tools.ietf.org/html/rfc3986#section-4.1
56
     * @return string
57
     */
58
    public function __toString()
59
    {
60
        return
61
            $this->getBasePart() .
62
            $this->getTailPart();
63
    }
64
65
    /**
66
     * Return an empty string or [scheme://][user:password@]host[:port]
67
     *
68
     * @return string
69
     */
70
    private function getBasePart(): string
71
    {
72
        $scheme = empty($this->getScheme()) ? '' : $this->getScheme() . ':';
73
        $authority = empty($this->getAuthority()) ? '' : '//' . $this->getAuthority();
74
75
        return $scheme . $authority;
76
    }
77
78
    /**
79
     * Return an empty string or /[path][?query][#fragment]
80
     *
81
     * @return string
82
     */
83
    private function getTailPart(): string
84
    {
85
        $path = '/';
86
87
        if (!empty($this->getPath())) {
88
            $path = strpos($this->getPath(), '/') === 0 ? '' : '/';
89
            $path .= $this->getPath();
90
        }
91
92
        $query = empty($this->getQuery()) ? '' : '?' . $this->getQuery();
93
        $fragment = empty($this->getFragment()) ? '' : '#' . $this->getFragment();
94
95
        return $path . $query . $fragment;
96
    }
97
}
98