Completed
Push — develop ( 0050db...7fb870 )
by Risan Bagja
01:33
created

UriConfig::build()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 1
1
<?php
2
3
namespace Risan\OAuth1\Config;
4
5
use GuzzleHttp\Psr7\Uri;
6
use InvalidArgumentException;
7
use GuzzleHttp\Psr7\UriResolver;
8
9
class UriConfig implements UriConfigInterface
0 ignored issues
show
Bug introduced by
There is one abstract method parser in this class; you could implement it, or declare this class as abstract.
Loading history...
10
{
11
    /**
12
     * The UriParserInterface implementation.
13
     *
14
     * @return \Risan\OAuth1\UriParserInterface
15
     */
16
    protected $parser;
17
18
    /**
19
     * The base URI.
20
     *
21
     * @var \Psr\Http\Message\UriInterface|null
22
     */
23
    protected $base;
24
25
    /**
26
     * The URI for obtaining temporary credentials. Also known as request token
27
     * URI.
28
     *
29
     * @var \Psr\Http\Message\UriInterface
30
     */
31
    protected $temporaryCredentials;
32
33
    /**
34
     * The URI for asking user to authorize the request.
35
     *
36
     * @var \Psr\Http\Message\UriInterface
37
     */
38
    protected $authorization;
39
40
    /**
41
     * The URI for obtaining token credentials. Also known as access token
42
     * URI.
43
     *
44
     * @var \Psr\Http\Message\UriInterface
45
     */
46
    protected $tokenCredentials;
47
48
    /**
49
     * The callback URI.
50
     *
51
     * @var \Psr\Http\Message\UriInterface|null
52
     */
53
    protected $callback;
54
55
    /**
56
     * Create UriConfig instance.
57
     *
58
     * @param array $uris
59
     * @param \Risan\OAuth1\UriParserInterface
60
     */
61
    public function __construct(array $uris, UriParserInterface $parser)
62
    {
63
        $this->setFromArray($uris);
64
        $this->parser = $parser;
65
    }
66
67
    /**
68
     * Set URIs from an array.
69
     *
70
     * @param array $uris
71
     * @return \Risan\OAuth1\Config\UriConfig
72
     */
73
    public function setFromArray(array $uris)
74
    {
75
        $this->validateUris($uris);
76
77
        $this->temporaryCredentials = $this->parser->toPsrUri($uris['temporary_credentials_uri']);
78
        $this->authorization = $this->parser->toPsrUri($uris['authorization_uri']);
79
        $this->tokenCredentials = $this->parser->toPsrUri($uris['token_credentials_uri']);
80
81
        if (isset($uris['base_uri'])) {
82
            $this->setBase($this->parser->toPsrUri($uris['base_uri']));
83
        }
84
85
        if (isset($uris['callback'])) {
86
            $this->callback = $this->parser->toPsrUri($uris['callback_uri']);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * Validate the given URI array.
94
     *
95
     * @param  array  $uris
96
     * @return boolean
97
     * @throws \InvalidArgumentException
98
     */
99
    public function validateUris(array $uris)
100
    {
101
        $requiredParams = [
102
            'temporary_credentials_uri',
103
            'authorization_uri',
104
            'token_credentials_uri',
105
        ];
106
107
        foreach ($requiredParams as $param) {
108
            if (! isset($uris[$param])) {
109
                throw new InvalidArgumentException("Missing URI configuration: {$param}.");
110
            }
111
        }
112
113
        return true;
114
    }
115
116
    /**
117
     * Set the base URI.
118
     *
119
     * @param  \Psr\Http\Message\UriInterface $uri
120
     * @return \Risan\OAuth1\Config\UriConfig
121
     * @throws \InvalidArgumentException
122
     */
123
    public function setBase(UriInterface $uri)
124
    {
125
        if (! $this->parser->isAbsolute($uri)) {
126
            throw new InvalidArgumentException('The base URI must be absolute.');
127
        }
128
129
        $this->base = $uri;
0 ignored issues
show
Documentation Bug introduced by
It seems like $uri of type object<Risan\OAuth1\Config\UriInterface> is incompatible with the declared type object<Psr\Http\Message\UriInterface>|null of property $base.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
130
131
        return $this;
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137
    public function base()
138
    {
139
        return $this->hasBase() ? $this->base : null;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145
    public function hasBase()
146
    {
147
        return $this->base !== null;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function temporaryCredentials()
154
    {
155
        return $this->build($this->temporaryCredentials);
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function authorization()
162
    {
163
        return $this->build($this->authorization);
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169
    public function tokenCredentials()
170
    {
171
        return $this->build($this->tokenCredentials);
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177
    public function callback()
178
    {
179
        return $this->hasCallback() ? $this->build($this->callback) : null;
0 ignored issues
show
Bug introduced by
It seems like $this->callback can be null; however, build() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    public function hasCallback()
186
    {
187
        return $this->callback !== null;
188
    }
189
190
    /**
191
     * Parse and build the given URI.
192
     *
193
     * @param  \Psr\Http\Message\UriInterface|string $uri
194
     * @return \Psr\Http\Message\UriInterface
195
     */
196
    public function build($uri)
197
    {
198
        $uri = $this->parser->toPsrUri($uri);
199
200
        if ($this->shouldBeResolvedToAbsoluteUri($uri)) {
201
            $uri = $this->parser->resolve($this->base(), $uri);
202
        }
203
204
        return $this->parser->isMissingScheme($uri) ? $uri->withScheme('http') : $uri;
205
    }
206
207
    /**
208
     * Check if the given URI should be resolved to absolute URI.
209
     *
210
     * @param  \Psr\Http\Message\UriInterface $uri
211
     * @return boolean
212
     */
213
    public function shouldBeResolvedToAbsoluteUri(UriInterface $uri)
214
    {
215
        return ! $this->parser->isAbsolute($uri) && $this->hasBase();
216
    }
217
}
218