Passed
Push — master ( 1d7587...a5dfb5 )
by Gabriel
02:49
created

GrantsTrait::makeGrantRefreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ByTIC\Hello\Oauth\ServiceProvider\Traits;
4
5
use ByTIC\Hello\Utility\ConfigHelper;
6
use DateInterval;
7
use League\OAuth2\Server\AuthorizationServer;
8
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
9
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
10
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
11
12
/**
13
 * Trait GrantsTrait
14
 * @package ByTIC\Hello\Oauth\ServiceProvider\Traits
15
 */
16
trait GrantsTrait
17
{
18
    /**
19
     * @param AuthorizationServer $server
20
     */
21 1
    public function registerGrants(AuthorizationServer $server)
22
    {
23 1
        $grantTypes = $this->getRegisteredGrants();
24 1
        foreach ($grantTypes as $type => $class) {
25 1
            $method = 'makeGrant' . $type;
26 1
            $this->{$method}($server, $class);
27
        }
28 1
    }
29
30
    /**
31
     * @return mixed|\Nip\Config\Config
32
     */
33
    protected function getRegisteredGrants()
34
    {
35
        return ConfigHelper::get('grant_types', []);
36
    }
37
38
    /**
39
     * @param AuthorizationServer $server
40
     * @param string $class
41
     */
42
    protected function makeGrantPersonalAccess(AuthorizationServer $server, string $class)
43
    {
44
        $this->makeGrant(
45
            $server,
46
            $class,
47
            [],
48
            new DateInterval('P1Y')
49
        );
50
    }
51
52
    /**
53
     * @param AuthorizationServer $server
54
     * @param string $class
55
     */
56
    protected function makeGrantAuthCode(AuthorizationServer $server, string $class)
57
    {
58
        $this->makeGrant(
59
            $server,
60
            $class,
61
            [
62
                $this->getContainer()->get(AuthCodeRepositoryInterface::class),
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
                $this->/** @scrutinizer ignore-call */ 
63
                       getContainer()->get(AuthCodeRepositoryInterface::class),
Loading history...
63
                $this->getContainer()->get(RefreshTokenRepositoryInterface::class),
64
                ConfigHelper::get('token_ttl')
65
            ]
66
        );
67
    }
68
69
    /**
70
     * @param AuthorizationServer $server
71
     * @param string $class
72
     */
73
    protected function makeGrantRefreshToken(AuthorizationServer $server, string $class)
74
    {
75
        $this->makeGrant($server, $class, [
76
            $this->getContainer()->get(RefreshTokenRepositoryInterface::class)
77
        ]);
78
    }
79
80
    /**
81
     * @param AuthorizationServer $server
82
     * @param string $class
83
     */
84
    protected function makeGrantPassword(AuthorizationServer $server, string $class)
85
    {
86
        $this->makeGrant($server, $class, [
87
            $this->getContainer()->get(UserRepositoryInterface::class),
88
            $this->getContainer()->get(RefreshTokenRepositoryInterface::class)
89
        ]);
90
    }
91
92
    /**
93
     * @param AuthorizationServer $server
94
     * @param string $class
95
     */
96
    protected function makeGrantImplicit(AuthorizationServer $server, string $class)
97
    {
98
        $this->makeGrant($server, $class, [
99
            ConfigHelper::get('token_ttl')
100
        ]);
101
    }
102
103
    /**
104
     * @param AuthorizationServer $server
105
     * @param string $class
106
     */
107
    protected function makeGrantClientCredentials(AuthorizationServer $server, string $class)
108
    {
109
        $this->makeGrant($server, $class, []);
110
    }
111
112
    /**
113
     * @param AuthorizationServer $server
114
     * @param string|null $type
115
     * @param array $args
116
     */
117
    protected function makeGrant(AuthorizationServer $server, string $type, array $args, $accessTokenTTL = null)
118
    {
119
        if ($type !== null) {
0 ignored issues
show
introduced by
The condition $type !== null is always true.
Loading history...
120
            $grant = new $type(...$args);
121
            if ($type !== null && $grant !== null) {
122
                $server->enableGrantType($grant, $accessTokenTTL);
123
            }
124
        }
125
    }
126
}
127