Passed
Push — master ( e9591a...c0a685 )
by Rutger
13:20
created

Oauth2GrantTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 36
c 1
b 0
f 0
dl 0
loc 70
ccs 25
cts 40
cp 0.625
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A issueRefreshToken() 0 18 2
A issueAuthCode() 0 22 2
A issueAccessToken() 0 21 2
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\server\grants\traits;
4
5
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
6
use League\OAuth2\Server\Entities\ClientEntityInterface;
7
use rhertogh\Yii2Oauth2Server\events\Oauth2AccessTokenIssuanceEvent;
8
use rhertogh\Yii2Oauth2Server\events\Oauth2AuthCodeIssuanceEvent;
9
use rhertogh\Yii2Oauth2Server\events\Oauth2RefreshTokenIssuanceEvent;
10
use rhertogh\Yii2Oauth2Server\Oauth2Module;
11
use Yii;
12
13
trait Oauth2GrantTrait
14
{
15
    /** @var Oauth2Module */
16
    public $module;
17
18
    protected function issueAuthCode(\DateInterval $authCodeTTL, ClientEntityInterface $client, $userIdentifier, $redirectUri, array $scopes = [])
19
    {
20
        /** @var Oauth2AuthCodeIssuanceEvent $event */
21
        $event = Yii::createObject([
22
            'class' => Oauth2AuthCodeIssuanceEvent::class,
23
            'grant' => $this,
24
            'authCodeTTL' => $authCodeTTL,
25
            'client' => $client,
26
            'userIdentifier' => $userIdentifier,
27
            'redirectUri' => $redirectUri,
28
            'scopes' => $scopes,
29
        ]);
30
31
        $this->module->trigger(Oauth2Module::EVENT_BEFORE_AUTH_CODE_ISSUANCE, $event);
32
33
        if (!$event->authCode) {
34
            $event->authCode = parent::issueAuthCode($authCodeTTL, $client, $userIdentifier, $redirectUri, $scopes);
35
        }
36
37
        $this->module->trigger(Oauth2Module::EVENT_AFTER_AUTH_CODE_ISSUANCE, $event);
38
39
        return $event->authCode;
40
    }
41
42 8
    protected function issueAccessToken(\DateInterval $accessTokenTTL, ClientEntityInterface $client, $userIdentifier, array $scopes = [])
43
    {
44
        /** @var Oauth2AccessTokenIssuanceEvent $event */
45 8
        $event = Yii::createObject([
46 8
            'class' => Oauth2AccessTokenIssuanceEvent::class,
47 8
            'grant' => $this,
48 8
            'accessTokenTTL' => $accessTokenTTL,
49 8
            'client' => $client,
50 8
            'userIdentifier' => $userIdentifier,
51 8
            'scopes' => $scopes,
52 8
        ]);
53
54 8
        $this->module->trigger(Oauth2Module::EVENT_BEFORE_ACCESS_TOKEN_ISSUANCE, $event);
55
56 8
        if (!$event->accessToken) {
57 8
            $event->accessToken = parent::issueAccessToken($accessTokenTTL, $client, $userIdentifier, $scopes); // TODO: Change the autogenerated stub
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
58
        }
59
60 8
        $this->module->trigger(Oauth2Module::EVENT_AFTER_ACCESS_TOKEN_ISSUANCE, $event);
61
62 8
        return $event->accessToken;
63
    }
64
65 4
    protected function issueRefreshToken(AccessTokenEntityInterface $accessToken)
66
    {
67
        /** @var Oauth2RefreshTokenIssuanceEvent $event */
68 4
        $event = Yii::createObject([
69 4
            'class' => Oauth2RefreshTokenIssuanceEvent::class,
70 4
            'grant' => $this,
71 4
            'accessToken' => $accessToken,
72 4
        ]);
73
74 4
        $this->module->trigger(Oauth2Module::EVENT_BEFORE_REFRESH_TOKEN_ISSUANCE, $event);
75
76 4
        if (!$event->refreshToken) {
77 4
            $event->refreshToken = parent::issueRefreshToken($accessToken);
78
        }
79
80 4
        $this->module->trigger(Oauth2Module::EVENT_AFTER_REFRESH_TOKEN_ISSUANCE, $event);
81
82 4
        return $event->refreshToken;
83
    }
84
}
85