Completed
Push — master ( f5d9cb...35a455 )
by Gabriel
02:54
created

OAuth2Middleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Somoza\OAuth2Middleware;
4
5
use Assert\Assertion;
6
use Psr\Http\Message\RequestInterface;
7
use Somoza\OAuth2Middleware\TokenService\AuthorizesRequests;
8
9
/**
10
 * @author Gabriel Somoza <[email protected]>
11
 */
12
final class OAuth2Middleware
13
{
14
    /** @var AuthorizesRequests */
15
    private $tokenService;
16
17
    /** @var string[] */
18
    private $ignoredUris;
19
20
    /**
21
     * Middleware constructor.
22
     * @param AuthorizesRequests $tokenService
23
     * @param \string[] $ignoredUris
24
     */
25 5
    public function __construct(AuthorizesRequests $tokenService, array $ignoredUris = [])
26
    {
27 5
        Assertion::allString($ignoredUris);
28 4
        $this->ignoredUris = $ignoredUris;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ignoredUris of type array<integer,object<string>> is incompatible with the declared type array<integer,string> of property $ignoredUris.

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...
29 4
        $this->tokenService = $tokenService;
30 4
    }
31
32
33
    /**
34
     * @param callable $handler
35
     * @return \Closure
36
     */
37
    public function __invoke(callable $handler): \Closure
38
    {
39 3
        return function (RequestInterface $request, array $options) use ($handler) {
40 2
            $uri = (string) $request->getUri();
41 2
            if (!$this->shouldSkipAuthorizationForUri($uri)) {
42 1
                $request = $this->tokenService->authorize($request);
43
            }
44
45 2
            return $handler($request, $options);
46 3
        };
47
    }
48
49
    /**
50
     * Returns whether a URL must NOT be authorized
51
     *
52
     * @param string $uri
53
     * @return bool
54
     */
55 2
    private function shouldSkipAuthorizationForUri(string $uri): bool
56
    {
57 2
        return in_array($uri, $this->ignoredUris);
58
    }
59
}
60