Passed
Push — master ( 056d39...c33903 )
by Gabriel
04:13
created

TokenFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 8
eloc 25
c 3
b 2
f 1
dl 0
loc 98
ccs 24
cts 27
cp 0.8889
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findAccessToken() 0 4 1
A dispatchRequestToAuthorizationServer() 0 8 1
A createRequest() 0 8 1
A make() 0 9 1
A __construct() 0 5 4
1
<?php
2
3
namespace ByTIC\Hello\Models\Clients\PersonalAccess;
4
5
use ByTIC\Hello\Models\AccessTokens\Token;
6
use ByTIC\Hello\Models\Clients\Client;
7
use ByTIC\Hello\Utility\ModelsHelper;
8
use Laminas\Diactoros\Response;
9
use Laminas\Diactoros\ServerRequest;
10
use Lcobucci\JWT\Parser as JwtParser;
11
use League\OAuth2\Server\AuthorizationServer;
12
use Nip\Container\Container;
13
14
/**
15
 * Class TokenFactory
16
 * @package ByTIC\Hello\Models\Clients\PersonalAccess
17
 */
18
class TokenFactory
19
{
20
    /**
21
     * The authorization server instance.
22
     *
23
     * @var \League\OAuth2\Server\AuthorizationServer
24
     */
25
    protected $server;
26
27
    /**
28
     * @var Client
29
     */
30
    protected $client;
31
32
    /**
33
     * @var Client
34
     */
35
    protected $jwt;
36
37
    /**
38
     * TokenFactory constructor
39
     *
40
     * @param \League\OAuth2\Server\AuthorizationServer $server
41
     * @param $client
42
     */
43 1
    public function __construct(AuthorizationServer $server = null, $client = null, JwtParser $jwt = null)
44
    {
45 1
        $this->server = $server ? $server : Container::getInstance()->get(AuthorizationServer::class);
46 1
        $this->client = $client ? $client : ClientsManager::get();
47 1
        $this->jwt = $jwt ? $jwt : new JwtParser();
0 ignored issues
show
Documentation Bug introduced by
It seems like $jwt ? $jwt : new Lcobucci\JWT\Parser() of type Lcobucci\JWT\Parser is incompatible with the declared type ByTIC\Hello\Models\Clients\Client of property $jwt.

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...
48 1
    }
49
50
51
    /**
52
     * Create a new personal access token.
53
     *a
54
     * @param mixed $userId
55
     * @param string $name
56
     * @param array $scopes
57
     * @return \Laravel\Passport\PersonalAccessTokenResult
0 ignored issues
show
Bug introduced by
The type Laravel\Passport\PersonalAccessTokenResult was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
     */
59 1
    public function make($userId, $name, array $scopes = [])
60
    {
61 1
        $response = $this->dispatchRequestToAuthorizationServer(
62 1
            $this->createRequest($this->client, $userId, $scopes)
63
        );
64
65 1
        $token = $this->findAccessToken($response);
66
67 1
        return $token;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $token returns the type ByTIC\Hello\Models\AccessTokens\Token which is incompatible with the documented return type Laravel\Passport\PersonalAccessTokenResult.
Loading history...
68
    }
69
70
    /**
71
     * Create a request instance for the given client.
72
     *
73
     * @param Client $client
74
     * @param mixed $userId
75
     * @param array $scopes
76
     * @return Request
0 ignored issues
show
Bug introduced by
The type ByTIC\Hello\Models\Clients\PersonalAccess\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
     */
78 1
    protected function createRequest($client, $userId, array $scopes)
79
    {
80 1
        return (new ServerRequest())->withParsedBody([
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Laminas\Diact...implode(' ', $scopes))) returns the type Laminas\Diactoros\ServerRequest which is incompatible with the documented return type ByTIC\Hello\Models\Clients\PersonalAccess\Request.
Loading history...
81 1
            'grant_type' => 'personal_access',
82 1
            'client_id' => $client->getIdentifier(),
83 1
            'client_secret' => $client->getSecret(),
84 1
            'user_id' => $userId,
85 1
            'scope' => implode(' ', $scopes),
86
        ]);
87
    }
88
89
    /**
90
     * Dispatch the given request to the authorization server.
91
     *
92
     * @param Request $request
93
     * @return array
94
     */
95 1
    protected function dispatchRequestToAuthorizationServer(ServerRequest $request)
96
    {
97 1
        return json_decode(
98 1
            $this->server->respondToAccessTokenRequest(
99 1
                $request,
100 1
                new Response()
101 1
            )->getBody()->__toString(),
102 1
            true
103
        );
104
    }
105
106
    /**
107
     * Get the access token instance for the parsed response.
108
     *
109
     * @param array $response
110
     * @return Token
111
     */
112
    protected function findAccessToken(array $response)
113
    {
114
        return ModelsHelper::accessTokens()->getByIdentifier(
0 ignored issues
show
Bug Best Practice introduced by
The expression return ByTIC\Hello\Utili...en'])->getClaim('jti')) returns the type Nip\Records\AbstractMode...ections\Collection|true which is incompatible with the documented return type ByTIC\Hello\Models\AccessTokens\Token.
Loading history...
115
            $this->jwt->parse($response['access_token'])->getClaim('jti')
0 ignored issues
show
Bug introduced by
The method parse() does not exist on ByTIC\Hello\Models\Clients\Client. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

115
            $this->jwt->/** @scrutinizer ignore-call */ 
116
                        parse($response['access_token'])->getClaim('jti')
Loading history...
Bug introduced by
The method getClaim() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

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

115
            $this->jwt->parse($response['access_token'])->/** @scrutinizer ignore-call */ getClaim('jti')

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...
116
        );
117
    }
118
}
119