Passed
Pull Request — main (#22)
by Andrey
26:45 queued 11:50
created

HttpBuilder::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Concerns\Makeable;
6
7
/**
8
 * Based on Maxim Ellrion's code.
9
 *
10
 * @see https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704
11
 */
12
final class HttpBuilder
13
{
14
    use Makeable;
15
16
    protected $parsed = [];
17
18
    protected $components = [
19
        PHP_URL_SCHEME   => 'scheme',
20
        PHP_URL_HOST     => 'host',
21
        PHP_URL_PORT     => 'port',
22
        PHP_URL_USER     => 'user',
23
        PHP_URL_PASS     => 'pass',
24
        PHP_URL_QUERY    => 'query',
25
        PHP_URL_PATH     => 'path',
26
        PHP_URL_FRAGMENT => 'fragment',
27
    ];
28
29
    public function parse(string $url, int $component = -1): self
30
    {
31
        $component = $this->componentIndex($component);
32
        $key       = $this->componentKey($component);
33
34
        $component === -1 || empty($key)
35
            ? $this->parsed       = parse_url($url)
36
            : $this->parsed[$key] = parse_url($url, $component);
37
38
        return $this;
39
    }
40
41
    public function getScheme(): ?string
42
    {
43
        return $this->get('scheme');
44
    }
45
46
    public function getHost(): ?string
47
    {
48
        return $this->get('host');
49
    }
50
51
    public function getPort(): ?string
52
    {
53
        return $this->get('port');
54
    }
55
56
    public function getUser(): ?string
57
    {
58
        return $this->get('user');
59
    }
60
61
    public function getPassword(): ?string
62
    {
63
        return $this->get('pass');
64
    }
65
66
    public function getPath(): ?string
67
    {
68
        return $this->get('path');
69
    }
70
71
    public function getQuery(): ?string
72
    {
73
        return $this->get('query');
74
    }
75
76
    public function getFragment(): ?string
77
    {
78
        return $this->get('fragment');
79
    }
80
81
    public function compile(): string
82
    {
83
        return implode('', $this->prepare());
84
    }
85
86
    protected function prepare(): array
87
    {
88
        return [
89
            $this->getScheme() ? $this->getScheme() . '://' : '',
90
            $this->getUser(),
91
            $this->getPassword() ? $this->getPassword() . '@' : '',
92
            $this->getHost(),
93
            $this->getPort() ? ':' . $this->getPort() : '',
94
            $this->getPath() ? '/' . ltrim($this->getPath()) : '',
95
            $this->getQuery() ? '?' . $this->getQuery() : '',
96
            $this->getFragment() ? '#' . $this->getFragment() : '',
97
        ];
98
    }
99
100
    protected function get(string $key): ?string
101
    {
102
        return Arr::get($this->parsed, $key);
0 ignored issues
show
Bug Best Practice introduced by
The method Helldar\Support\Helpers\Arr::get() is not static, but was called statically. ( Ignorable by Annotation )

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

102
        return Arr::/** @scrutinizer ignore-call */ get($this->parsed, $key);
Loading history...
103
    }
104
105
    protected function componentIndex(int $component = -1): int
106
    {
107
        return isset($this->components[$component]) ? $component : -1;
108
    }
109
110
    protected function componentKey(int $component = -1): ?string
111
    {
112
        return $this->components[$component] ?? null;
113
    }
114
}
115