TokenFactory::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 8
c 2
b 0
f 1
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.6111
cc 5
nc 12
nop 3
crap 5
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\Encoding\JoseEncoder;
0 ignored issues
show
Bug introduced by
The type Lcobucci\JWT\Encoding\JoseEncoder 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...
11
use Lcobucci\JWT\Token\RegisteredClaims;
12
use League\OAuth2\Server\AuthorizationServer;
13
use Nip\Container\Container;
14
15
/**
16
 * Class TokenFactory
17
 * @package ByTIC\Hello\Models\Clients\PersonalAccess
18
 */
19
class TokenFactory
20
{
21
    /**
22
     * The authorization server instance.
23
     *
24
     * @var \League\OAuth2\Server\AuthorizationServer
25
     */
26
    protected $server;
27
28
    /**
29
     * @var Client
30
     */
31
    protected $client;
32
33
    /**
34
     * @var Client
35
     */
36
    protected $jwt;
37
38
    /**
39
     * TokenFactory constructor
40
     *
41
     * @param \League\OAuth2\Server\AuthorizationServer $server
42
     * @param $client
43 1
     */
44
    public function __construct(AuthorizationServer $server = null, $client = null, \Lcobucci\JWT\Parser $jwt = null)
45 1
    {
46 1
        $this->server = $server ? $server : Container::getInstance()->get(AuthorizationServer::class);
47 1
        $this->client = $client ? $client : ClientsManager::get();
48 1
        if (!$jwt) {
49
            if (class_exists(\Lcobucci\JWT\Parser::class)) {
50
                $jwt = new \Lcobucci\JWT\Parser();
51
            } else {
52
                $jwt = new \Lcobucci\JWT\Token\Parser(new JoseEncoder());
0 ignored issues
show
Bug introduced by
The type Lcobucci\JWT\Token\Parser 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...
53
            }
54
        }
55
        $this->jwt = $jwt;
0 ignored issues
show
Documentation Bug introduced by
It seems like $jwt can also be of type Lcobucci\JWT\Parser. However, the property $jwt is declared as type ByTIC\Hello\Models\Clients\Client. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
56
    }
57
58
59 1
    /**
60
     * Create a new personal access token.
61 1
     *a
62 1
     * @param mixed $userId
63
     * @param string $name
64
     * @param array $scopes
65 1
     * @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...
66
     */
67 1
    public function make($userId, $name, array $scopes = [])
68
    {
69
        $response = $this->dispatchRequestToAuthorizationServer(
70
            $this->createRequest($this->client, $userId, $scopes)
71
        );
72
73
        $token = $this->findAccessToken($response);
74
75
        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...
76
    }
77
78 1
    /**
79
     * Create a request instance for the given client.
80 1
     *
81 1
     * @param Client $client
82 1
     * @param mixed $userId
83 1
     * @param array $scopes
84 1
     * @return ServerRequest|\Psr\Http\Message\ServerRequestInterface
85 1
     */
86
    protected function createRequest($client, $userId, array $scopes)
87
    {
88
        return (new ServerRequest())->withParsedBody([
89
            'grant_type' => 'personal_access',
90
            'client_id' => $client->getIdentifier(),
91
            'client_secret' => $client->getSecret(),
92
            'user_id' => $userId,
93
            'scope' => implode(' ', $scopes),
94
        ]);
95 1
    }
96
97 1
    /**
98 1
     * Dispatch the given request to the authorization server.
99 1
     *
100 1
     * @param ServerRequest $request
101 1
     * @return array
102 1
     */
103
    protected function dispatchRequestToAuthorizationServer(ServerRequest $request)
104
    {
105
        return json_decode(
106
            $this->server->respondToAccessTokenRequest(
107
                $request,
108
                new Response()
109
            )->getBody()->__toString(),
110
            true
111
        );
112
    }
113
114
    /**
115
     * Get the access token instance for the parsed response.
116
     *
117
     * @param array $response
118
     * @return Token
119
     */
120
    protected function findAccessToken(array $response)
121
    {
122
        $token = $this->jwt->parse($response['access_token']);
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

122
        /** @scrutinizer ignore-call */ 
123
        $token = $this->jwt->parse($response['access_token']);
Loading history...
123
124
        return ModelsHelper::accessTokens()->getByIdentifier(
0 ignored issues
show
Bug Best Practice introduced by
The expression return ByTIC\Hello\Utili...\RegisteredClaims::ID)) returns the type Nip\Records\AbstractMode...ections\Collection|true which is incompatible with the documented return type ByTIC\Hello\Models\AccessTokens\Token.
Loading history...
125
            $token->claims()->get(RegisteredClaims::ID)
0 ignored issues
show
Bug introduced by
The method get() does not exist on Nip\Helpers\AbstractHelper. It seems like you code against a sub-type of Nip\Helpers\AbstractHelper such as Nip_Helper_Url or Nip_Helper_Arrays or Nip\Helpers\View\Stylesheets or Nip\Helpers\View\Strings or Nip\Helpers\View\Paginator or Nip\Helpers\View\Arrays or Nip\Helpers\View\Icontext or Nip\Helpers\View\Color or Nip\Helpers\View\Scripts or Nip\Helpers\View\Url. ( Ignorable by Annotation )

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

125
            $token->claims()->/** @scrutinizer ignore-call */ get(RegisteredClaims::ID)
Loading history...
Bug introduced by
The method claims() 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

125
            $token->/** @scrutinizer ignore-call */ 
126
                    claims()->get(RegisteredClaims::ID)

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...
126
        );
127
    }
128
}
129