Completed
Push — master ( 268d58...7f1d79 )
by Boris
13:24
created

alwaysRequireAClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * @author Boris Guéry <[email protected]>
4
 */
5
6
namespace Bgy\OAuth2;
7
8
use Bgy\OAuth2\GrantType\GrantType;
9
use Bgy\OAuth2\Storage\AccessTokenStorage;
10
use Bgy\OAuth2\Storage\ClientStorage;
11
use Bgy\OAuth2\Storage\RefreshTokenStorage;
12
13
class AuthorizationServerConfigurationBuilder
14
{
15
    private $defaultOptions = [
16
        'client_storage'                  => null,
17
        'access_token_storage'            => null,
18
        'refresh_token_storage'           => null,
19
        'access_token_generator'          => null,
20
        'always_require_a_client'         => false,
21
        'access_token_length'             => 32,
22
        'access_token_ttl'                => 3600,
23
        'always_generate_a_refresh_token' => false,
24
        'grant_types'                     => []
25
    ];
26
27
    private $options = [
28
        'client_storage'                  => null,
29
        'access_token_storage'            => null,
30
        'refresh_token_storage'           => null,
31
        'access_token_generator'          => null,
32
        'always_require_a_client'         => false,
33
        'access_token_length'             => 32,
34
        'access_token_ttl'                => 3600,
35
        'always_generate_a_refresh_token' => false,
36
        'grant_types'                     => []
37
    ];
38
39
    private $built = false;
40
41
    private $configuration;
42
43
    public function __construct()
44
    {
45
    }
46
47
    public function setClientStorage(ClientStorage $clientStorage)
48
    {
49
        $this->options['client_storage'] = $clientStorage;
50
51
        return $this;
52
    }
53
54
    public function setAccessTokenStorage(AccessTokenStorage $clientStorage)
55
    {
56
        $this->options['access_token_storage'] = $clientStorage;
57
58
        return $this;
59
    }
60
61
    public function setRefreshStorage(RefreshTokenStorage $clientStorage)
62
    {
63
        $this->options['refresh_token_storage'] = $clientStorage;
64
65
        return $this;
66
    }
67
68
    public function alwaysRequireAClient($flag)
69
    {
70
        $this->options['always_require_a_client'] = (bool) $flag;
71
72
        return $this;
73
    }
74
75
    public function alwaysGenerateARefreshToken($flag)
76
    {
77
        $this->options['always_generate_a_refresh_token'] = (bool) $flag;
78
79
        return $this;
80
    }
81
82
    public function setAccessTokenTTL($ttlInSeconds)
83
    {
84
        $this->options['access_token_ttl'] = (int) $ttlInSeconds;
85
86
        return $this;
87
88
    }
89
90
    public function setAccessTokenGenerator($accessTokenGenerator)
91
    {
92
        $this->options['access_token_generator'] = $accessTokenGenerator;
93
94
        return $this;
95
    }
96
97
    public function addGrantType(GrantType $grantType, $overrideExisting = false)
98
    {
99
        if (isset($this->options['grant_types'][$grantType->getIdentifier()]) && !$overrideExisting) {
100
101
            throw UnableToAddGrantType::aGrantTypeExtensionIsAlreadyRegisteredForThisIdentifier(
102
                $grantType->getIdentifier(),
103
                get_class($this->options['grant_types'][$grantType->getIdentifier()])
104
            );
105
        }
106
107
        $this->options['grant_types'][$grantType->getIdentifier()] = $grantType;
108
109
        return $this;
110
    }
111
112
    public function build()
113
    {
114
        // all options without a default value (not null) are required
115
        $missingOptions  = array_filter($this->options, function ($value, $key) {
116
            return (null === $value);
117
        }, ARRAY_FILTER_USE_BOTH);
118
119
        if (count($missingOptions) > 0) {
120
121
            throw new \RuntimeException(sprintf(
122
                'You must configure the following options: %s',
123
                rtrim(implode(", ", array_keys($missingOptions)), ", ")
124
            ));
125
        }
126
127
        $this->built = true;
128
129
        $this->configuration = new AuthorizationServerConfiguration(
130
            $this->options['client_storage'],
131
            $this->options['access_token_storage'],
132
            $this->options['refresh_token_storage'],
133
            $this->options['grant_types'],
134
            $this->options['access_token_generator'],
135
            [
136
                'always_require_a_client'         => $this->options['always_require_a_client'],
137
                'access_token_ttl'                => $this->options['access_token_ttl'],
138
                'access_token_length'             => $this->options['access_token_length'],
139
                'always_generate_a_refresh_token' => $this->options['always_generate_a_refresh_token'],
140
            ]
141
        );
142
143
        return $this;
144
    }
145
146
    public function reset()
147
    {
148
        $this->options = $this->defaultOptions;
149
150
        $this->built = false;
151
    }
152
153
    public function getAuthorizationServerConfiguration()
154
    {
155
        if (!$this->built) {
156
157
            throw new \LogicException('You must build() the configuration before to get it.');
158
        }
159
160
        return $this->configuration;
161
    }
162
}
163