JWTFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 0
loc 39
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getForAccessToken() 0 16 1
A getScopesAsList() 0 9 2
1
<?php
2
3
4
namespace Mvdstam\Oauth2ServerLaravel\Factories;
5
6
7
use Carbon\Carbon;
8
use Lcobucci\JWT\Builder;
9
use Lcobucci\JWT\Signer\Key;
10
use Lcobucci\JWT\Signer\Rsa\Sha256;
11
use League\OAuth2\Server\CryptKey;
12
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
13
use Mvdstam\Oauth2ServerLaravel\Contracts\JWTFactoryInterface;
14
15
class JWTFactory implements JWTFactoryInterface
16
{
17
18
    /**
19
     * @inheritdoc
20
     */
21 11
    public function getForAccessToken(AccessTokenEntityInterface $accessToken, CryptKey $privateKey)
22
    {
23 11
        $now = Carbon::now();
24
25 11
        return (new Builder)
26 11
            ->setIssuer(url('/'))
0 ignored issues
show
Bug introduced by
It seems like url('/') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Lcobucci\JWT\Builder::setIssuer() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
27 11
            ->setIssuedAt($now->timestamp)
28 11
            ->setNotBefore($now->timestamp)
29 11
            ->setId($accessToken->getIdentifier())
30 11
            ->setExpiration($accessToken->getExpiryDateTime()->getTimestamp())
31 11
            ->setAudience($accessToken->getClient()->getIdentifier())
32 11
            ->setSubject($accessToken->getUserIdentifier())
33 11
            ->set('scopes', implode(',', $this->getScopesAsList($accessToken)))
34 11
            ->sign(new Sha256, new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()))
35 11
            ->getToken();
36
    }
37
38
39
    /**
40
     * @param AccessTokenEntityInterface $accessToken
41
     * @return string[]
42
     */
43 11
    public function getScopesAsList(AccessTokenEntityInterface $accessToken)
44
    {
45 11
        $scopes = [];
46 11
        foreach ($accessToken->getScopes() as $scope) {
47 4
            $scopes[] = $scope->getIdentifier();
48
        }
49
50 11
        return $scopes;
51
    }
52
53
}
54