Completed
Pull Request — master (#4)
by Risan Bagja
02:18 queued 51s
created

UriConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Risan\OAuth1\Config;
4
5
use InvalidArgumentException;
6
use Psr\Http\Message\UriInterface;
7
use Risan\OAuth1\Request\UriParserInterface;
8
9
class UriConfig implements UriConfigInterface
10
{
11
    /**
12
     * The UriParserInterface implementation.
13
     *
14
     * @return \Risan\OAuth1\Request\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\Request\UriParserInterface $parser
60
     */
61
    public function __construct(array $uris, UriParserInterface $parser)
62
    {
63
        $this->parser = $parser;
64
        $this->setFromArray($uris);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getParser()
71
    {
72
        return $this->parser;
73
    }
74
75
    /**
76
     * Set URIs from an array.
77
     *
78
     * @param array $uris
79
     * @return \Risan\OAuth1\Config\UriConfig
80
     */
81
    public function setFromArray(array $uris)
82
    {
83
        $this->validateUris($uris);
84
85
        $this->temporaryCredentials = $this->parser->toPsrUri($uris['temporary_credentials_uri']);
86
        $this->authorization = $this->parser->toPsrUri($uris['authorization_uri']);
87
        $this->tokenCredentials = $this->parser->toPsrUri($uris['token_credentials_uri']);
88
89
        if (isset($uris['base_uri'])) {
90
            $this->setBase($this->parser->toPsrUri($uris['base_uri']));
91
        }
92
93
        if (isset($uris['callback_uri'])) {
94
            $this->callback = $this->parser->toPsrUri($uris['callback_uri']);
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * Validate the given URI array.
102
     *
103
     * @param  array  $uris
104
     * @return boolean
105
     * @throws \InvalidArgumentException
106
     */
107
    public function validateUris(array $uris)
108
    {
109
        $requiredParams = [
110
            'temporary_credentials_uri',
111
            'authorization_uri',
112
            'token_credentials_uri',
113
        ];
114
115
        foreach ($requiredParams as $param) {
116
            if (! isset($uris[$param])) {
117
                throw new InvalidArgumentException("Missing URI configuration: {$param}.");
118
            }
119
        }
120
121
        return true;
122
    }
123
124
    /**
125
     * Set the base URI.
126
     *
127
     * @param  \Psr\Http\Message\UriInterface $uri
128
     * @return \Risan\OAuth1\Config\UriConfig
129
     * @throws \InvalidArgumentException
130
     */
131
    public function setBase(UriInterface $uri)
132
    {
133
        if (! $this->parser->isAbsolute($uri)) {
134
            throw new InvalidArgumentException('The base URI must be absolute.');
135
        }
136
137
        $this->base = $uri;
138
139
        return $this;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145
    public function base()
146
    {
147
        return $this->hasBase() ? $this->base : null;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function hasBase()
154
    {
155
        return $this->base !== null;
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function forTemporaryCredentials()
162
    {
163
        return $this->build($this->temporaryCredentials);
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169
    public function forAuthorization()
170
    {
171
        return $this->build($this->authorization);
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177
    public function forTokenCredentials()
178
    {
179
        return $this->build($this->tokenCredentials);
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    public function callback()
186
    {
187
        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...
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    public function hasCallback()
194
    {
195
        return $this->callback !== null;
196
    }
197
198
    /**
199
     * Parse and build the given URI.
200
     *
201
     * @param  \Psr\Http\Message\UriInterface|string $uri
202
     * @return \Psr\Http\Message\UriInterface
203
     */
204
    public function build($uri)
205
    {
206
        $uri = $this->parser->toPsrUri($uri);
207
208
        if ($this->shouldBeResolvedToAbsoluteUri($uri)) {
209
            $uri = $this->parser->resolve($this->base(), $uri);
0 ignored issues
show
Bug introduced by
It seems like $this->base() can be null; however, resolve() 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...
210
        }
211
212
        return $this->parser->isMissingScheme($uri) ? $uri->withScheme('http') : $uri;
213
    }
214
215
    /**
216
     * Check if the given URI should be resolved to absolute URI.
217
     *
218
     * @param  \Psr\Http\Message\UriInterface $uri
219
     * @return boolean
220
     */
221
    public function shouldBeResolvedToAbsoluteUri(UriInterface $uri)
222
    {
223
        return ! $this->parser->isAbsolute($uri) && $this->hasBase();
224
    }
225
}
226