ResourceServer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A validateAuthenticatedRequest() 0 3 1
A getAuthorizationValidator() 0 11 3
1
<?php
2
3
/**
4
 * @author      Alex Bilbie <[email protected]>
5
 * @copyright   Copyright (c) Alex Bilbie
6
 * @license     http://mit-license.org/
7
 *
8
 * @link        https://github.com/thephpleague/oauth2-server
9
 */
10
11
declare(strict_types=1);
12
13
namespace League\OAuth2\Server;
14
15
use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface;
16
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator;
17
use League\OAuth2\Server\Exception\OAuthServerException;
18
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
class ResourceServer
22
{
23
    private CryptKeyInterface $publicKey;
24
25
    private ?AuthorizationValidatorInterface $authorizationValidator = null;
26
27 4
    public function __construct(
28
        private AccessTokenRepositoryInterface $accessTokenRepository,
29
        CryptKeyInterface|string $publicKey,
30
        AuthorizationValidatorInterface $authorizationValidator = null
31
    ) {
32 4
        if ($publicKey instanceof CryptKeyInterface === false) {
33 4
            $publicKey = new CryptKey($publicKey);
34
        }
35 4
        $this->publicKey = $publicKey;
36
37 4
        $this->authorizationValidator = $authorizationValidator;
38
    }
39
40 4
    protected function getAuthorizationValidator(): AuthorizationValidatorInterface
41
    {
42 4
        if ($this->authorizationValidator instanceof AuthorizationValidatorInterface === false) {
43 4
            $this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository);
44
        }
45
46 4
        if ($this->authorizationValidator instanceof BearerTokenValidator === true) {
47 4
            $this->authorizationValidator->setPublicKey($this->publicKey);
0 ignored issues
show
Bug introduced by
The method setPublicKey() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $this->authorizationValidator->/** @scrutinizer ignore-call */ 
48
                                           setPublicKey($this->publicKey);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setPublicKey() does not exist on League\OAuth2\Server\Aut...ationValidatorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to League\OAuth2\Server\Aut...ationValidatorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $this->authorizationValidator->/** @scrutinizer ignore-call */ 
48
                                           setPublicKey($this->publicKey);
Loading history...
48
        }
49
50 4
        return $this->authorizationValidator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->authorizationValidator could return the type null which is incompatible with the type-hinted return League\OAuth2\Server\Aut...ationValidatorInterface. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    /**
54
     * Determine the access token validity.
55
     *
56
     * @throws OAuthServerException
57
     */
58 4
    public function validateAuthenticatedRequest(ServerRequestInterface $request): ServerRequestInterface
59
    {
60 4
        return $this->getAuthorizationValidator()->validateAuthorization($request);
61
    }
62
}
63