Completed
Push — develop ( 7fa98c...fc74bf )
by Risan Bagja
01:47
created

UriConfig::authorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 14
    public function __construct(array $uris, UriParserInterface $parser)
62
    {
63 14
        $this->parser = $parser;
64 14
        $this->setFromArray($uris);
65 14
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 1
    public function getParser()
71
    {
72 1
        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 14
    public function setFromArray(array $uris)
82
    {
83 14
        $this->validateUris($uris);
84
85 14
        $this->temporaryCredentials = $this->parser->toPsrUri($uris['temporary_credentials_uri']);
86 14
        $this->authorization = $this->parser->toPsrUri($uris['authorization_uri']);
87 14
        $this->tokenCredentials = $this->parser->toPsrUri($uris['token_credentials_uri']);
88
89 14
        if (isset($uris['base_uri'])) {
90 14
            $this->setBase($this->parser->toPsrUri($uris['base_uri']));
91
        }
92
93 14
        if (isset($uris['callback_uri'])) {
94 14
            $this->callback = $this->parser->toPsrUri($uris['callback_uri']);
95
        }
96
97 14
        return $this;
98
    }
99
100
    /**
101
     * Validate the given URI array.
102
     *
103
     * @param  array  $uris
104
     * @return boolean
105
     * @throws \InvalidArgumentException
106
     */
107 14
    public function validateUris(array $uris)
108
    {
109
        $requiredParams = [
110 14
            'temporary_credentials_uri',
111
            'authorization_uri',
112
            'token_credentials_uri',
113
        ];
114
115 14
        foreach ($requiredParams as $param) {
116 14
            if (! isset($uris[$param])) {
117 14
                throw new InvalidArgumentException("Missing URI configuration: {$param}.");
118
            }
119
        }
120
121 14
        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 14
    public function setBase(UriInterface $uri)
132
    {
133 14
        if (! $this->parser->isAbsolute($uri)) {
134 1
            throw new InvalidArgumentException('The base URI must be absolute.');
135
        }
136
137 14
        $this->base = $uri;
138
139 14
        return $this;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145 7
    public function base()
146
    {
147 7
        return $this->hasBase() ? $this->base : null;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153 9
    public function hasBase()
154
    {
155 9
        return $this->base !== null;
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161 2
    public function temporaryCredentials()
162
    {
163 2
        return $this->build($this->temporaryCredentials);
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169 2
    public function authorization()
170
    {
171 2
        return $this->build($this->authorization);
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177 2
    public function tokenCredentials()
178
    {
179 2
        return $this->build($this->tokenCredentials);
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185 2
    public function callback()
186
    {
187 2
        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 3
    public function hasCallback()
194
    {
195 3
        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 6
    public function build($uri)
205
    {
206 6
        $uri = $this->parser->toPsrUri($uri);
207
208 6
        if ($this->shouldBeResolvedToAbsoluteUri($uri)) {
209 5
            $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 6
        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 7
    public function shouldBeResolvedToAbsoluteUri(UriInterface $uri)
222
    {
223 7
        return ! $this->parser->isAbsolute($uri) && $this->hasBase();
224
    }
225
}
226