Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

TokenFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
ccs 23
cts 26
cp 0.8846
wmc 8
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 4
A make() 0 10 1
A createRequest() 0 10 1
A dispatchRequestToAuthorizationServer() 0 9 1
A findAccessToken() 0 6 1
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 Firebase\JWT\JWT;
9
use League\OAuth2\Server\AuthorizationServer;
10
use Lcobucci\JWT\Parser as JwtParser;
11
use Nip\Container\Container;
12
use Zend\Diactoros\ServerRequest;
13
use Zend\Diactoros\Response;
14
15
/**
16
 * Class TokenFactory
17
 * @package ByTIC\Hello\Models\Clients\PersonalAccess
18
 */
19
class TokenFactory
20
{
21
22
    /**
23
     * The authorization server instance.
24
     *
25
     * @var \League\OAuth2\Server\AuthorizationServer
26
     */
27
    protected $server;
28
29
    /**
30
     * @var Client
31
     */
32
    protected $client;
33
34
    /**
35
     * @var Client
36
     */
37
    protected $jwt;
38
39
    /**
40
     * TokenFactory constructor
41
     *
42
     * @param \League\OAuth2\Server\AuthorizationServer $server
43
     * @param $client
44
     */
45 1
    public function __construct(AuthorizationServer $server = null, $client = null, JwtParser $jwt = null)
46
    {
47 1
        $this->server = $server ? $server : Container::getInstance()->get(AuthorizationServer::class);
48 1
        $this->client = $client ? $client : ClientsManager::get();
49 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 object<Lcobucci\JWT\Parser> is incompatible with the declared type object<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...
50 1
    }
51
52
53
    /**
54
     * Create a new personal access token.
55
     *a
56
     * @param mixed $userId
57
     * @param string $name
58
     * @param array $scopes
59
     * @return \Laravel\Passport\PersonalAccessTokenResult
60
     */
61 1
    public function make($userId, $name, array $scopes = [])
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63 1
        $response = $this->dispatchRequestToAuthorizationServer(
64 1
            $this->createRequest($this->client, $userId, $scopes)
65
        );
66
67 1
        $token = $this->findAccessToken($response);
68
69 1
        return $token;
70
    }
71
72
    /**
73
     * Create a request instance for the given client.
74
     *
75
     * @param Client $client
76
     * @param mixed $userId
77
     * @param array $scopes
78
     * @return Request
79
     */
80 1
    protected function createRequest($client, $userId, array $scopes)
81
    {
82 1
        return (new ServerRequest())->withParsedBody([
83 1
            'grant_type' => 'personal_access',
84 1
            'client_id' => $client->getIdentifier(),
85 1
            'client_secret' => $client->getSecret(),
86 1
            'user_id' => $userId,
87 1
            'scope' => implode(' ', $scopes),
88
        ]);
89
    }
90
91
    /**
92
     * Dispatch the given request to the authorization server.
93
     *
94
     * @param Request $request
95
     * @return array
96
     */
97 1
    protected function dispatchRequestToAuthorizationServer(ServerRequest $request)
98
    {
99 1
        return json_decode(
100 1
            $this->server->respondToAccessTokenRequest(
101 1
                $request, new Response
102 1
            )->getBody()->__toString(),
103 1
            true
104
        );
105
    }
106
107
    /**
108
     * Get the access token instance for the parsed response.
109
     *
110
     * @param array $response
111
     * @return Token
112
     */
113
    protected function findAccessToken(array $response)
114
    {
115
        return ModelsHelper::accessTokens()->getByIdentifier(
116
            $this->jwt->parse($response['access_token'])->getClaim('jti')
0 ignored issues
show
Documentation Bug introduced by
The method parse does not exist on object<ByTIC\Hello\Models\Clients\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117
        );
118
    }
119
}
120