GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#38)
by Choraimy
16:03
created

Builder::parseUri()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 9.6111
1
<?php
2
3
namespace LaraCrafts\UrlShortener;
4
5
use GuzzleHttp\Psr7\Uri;
6
use InvalidArgumentException;
7
use LaraCrafts\UrlShortener\Concerns\CustomDomains;
8
use LaraCrafts\UrlShortener\Concerns\CustomSuffixes;
9
use LaraCrafts\UrlShortener\Concerns\ToString;
10
use LaraCrafts\UrlShortener\Concerns\ToUri;
11
use LaraCrafts\UrlShortener\Contracts\Client;
12
use LaraCrafts\UrlShortener\Contracts\UnsupportedOperationException;
13
use Psr\Http\Message\UriInterface;
14
15
class Builder
16
{
17
    protected $client;
18
    protected $options;
19
    protected $uri;
20
21
    /**
22
     * Create a new shortening builder instance.
23
     *
24
     * @param \LaraCrafts\UrlShortener\Contracts\Client $client
25
     * @param \Psr\Http\Message\UriInterface|string $uri
26
     * @return void
27
     */
28
    public function __construct(Client $client, $uri)
29
    {
30
        $this->client = $client;
31
        $this->options = [];
32
        $this->uri = $this->parseUri($uri);
33
    }
34
35
    /**
36
     * Set the domain to shorten to.
37
     *
38
     * @param string $domain
39
     * @return $this
40
     */
41
    public function domain(string $domain)
42
    {
43
        $driver = $this->getClient()->driver();
44
45
        if (!$driver instanceof CustomDomains) {
46
            throw new UnsupportedOperationException('Applying a custom domain is not supported');
47
        }
48
49
        $driver->withDomain($this, $domain);
50
51
        return $this;
52
    }
53
54
    /**
55
     * Get the shortened URI.
56
     *
57
     * @return \Psr\Http\Message\UriInterface
58
     */
59
    public function get()
60
    {
61
        $driver = $this->getClient()->driver();
62
63
        if ($driver instanceof ToUri) {
64
            $uri = $driver->toUri($this->uri, $this->options);
65
        } elseif ($driver instanceof ToString) {
66
            $uri = $driver->toString($this->uri, $this->options);
67
        } else {
68
            throw new UnsupportedOperationException('URL shortening is not supported');
69
        }
70
71
        return $this->parseUri($uri);
72
    }
73
74
    /**
75
     * Get the client.
76
     *
77
     * @return \LaraCrafts\UrlShortener\Contracts\Client
78
     */
79
    public function getClient()
80
    {
81
        return $this->client;
82
    }
83
84
    /**
85
     * Get the options.
86
     *
87
     * @return array
88
     */
89
    public function getOptions()
90
    {
91
        return $this->options;
92
    }
93
94
    /**
95
     * Get the URI.
96
     *
97
     * @return \Psr\Http\Message\UriInterface
98
     */
99
    public function getUri()
100
    {
101
        return $this->uri;
102
    }
103
104
    /**
105
     * Parse the given URI.
106
     *
107
     * @param \Psr\Http\Message\UriInterface|string $uri
108
     * @return \Psr\Http\Message\UriInterface
109
     */
110
    protected function parseUri($uri)
111
    {
112
        if ($uri instanceof UriInterface) {
113
            return $uri;
114
        }
115
116
        if (is_string($uri) || (is_object($uri) && method_exists($uri, '__toString'))) {
0 ignored issues
show
introduced by
The condition is_string($uri) is always true.
Loading history...
117
            return new Uri((string)$uri);
118
        }
119
120
        throw new InvalidArgumentException('URI must be a string or UriInterface');
121
    }
122
123
    /**
124
     * Set the suffix to shorten to.
125
     *
126
     * @param string $suffix
127
     * @return $this
128
     */
129
    public function to(string $suffix)
130
    {
131
        $driver = $this->getClient()->driver();
132
133
        if (!$driver instanceof CustomSuffixes) {
134
            throw new UnsupportedOperationException('Applying a custom suffix is not supported');
135
        }
136
137
        $driver->withSuffix($this, $suffix);
138
139
        return $this;
140
    }
141
142
    /**
143
     * Add a piece of data to the builder.
144
     *
145
     * @param array|string $key
146
     * @param mixed|null $value
147
     * @return $this
148
     */
149
    public function with($key, $value = null)
150
    {
151
        if (is_array($key)) {
152
            $this->options = array_merge($this->options, $key);
153
        } else {
154
            $this->options[$key] = $value;
155
        }
156
157
        return $this;
158
    }
159
160
    /**
161
     * Get the shortened URI as a string.
162
     *
163
     * @return string
164
     */
165
    public function __toString()
166
    {
167
        return (string)$this->get();
168
    }
169
}
170