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

GrantsTrait::makeGrant()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 4
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 20
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 2
    public function registerGrants(AuthorizationServer $server)
22
    {
23 2
        $grantTypes = $this->getRegisteredGrants();
24 2
        foreach ($grantTypes as $type => $class) {
25 1
            $method = 'makeGrant' . $type;
26 1
            $this->{$method}($server, $class);
27
        }
28 2
    }
29
30
    /**
31
     * @return mixed|\Nip\Config\Config
32
     */
33 1
    protected function getRegisteredGrants()
34
    {
35 1
        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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
                $this->getContainer()->get(RefreshTokenRepositoryInterface::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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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)
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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),
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
            $this->getContainer()->get(RefreshTokenRepositoryInterface::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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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) {
120
            $grant = new $type(...$args);
121
            if ($type !== null && $grant !== null) {
122
                $server->enableGrantType($grant, $accessTokenTTL);
123
            }
124
        }
125
    }
126
}
127