Completed
Push — master ( 7810d2...aebd05 )
by Alexandre
02:43
created

Server::__construct()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 30
nc 64
nop 8
dl 0
loc 51
ccs 0
cts 27
cp 0
crap 56
rs 6.9743
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 07/01/2018
6
 * Time: 13:57
7
 */
8
9
namespace OAuth2;
10
11
12
use OAuth2\ClientAuthentication\Guard;
13
use OAuth2\Endpoints\AuthorizationEndpoint;
14
use OAuth2\Endpoints\TokenEndpoint;
15
use OAuth2\Providers\ResourceOwnerProviderInterface;
16
use OAuth2\Repositories\ClientAuthenticatorRepository;
17
use OAuth2\Repositories\ConfigurationRepository;
18
use OAuth2\Repositories\GrantTypeRepository;
19
use OAuth2\Repositories\ResponseTypeRepository;
20
use OAuth2\Repositories\StorageRepository;
21
use OAuth2\ScopePolicy\Manager;
22
23
24
class Server
25
{
26
    /**
27
     * @var ConfigurationRepository
28
     */
29
    protected $configurationRepository;
30
    /**
31
     * @var StorageRepository
32
     */
33
    protected $storageRepository;
34
    /**
35
     * @var ResponseTypeRepository
36
     */
37
    protected $responseTypeRepository;
38
    /**
39
     * @var GrantTypeRepository
40
     */
41
    protected $grantTypeRepository;
42
    /**
43
     * @var ClientAuthenticatorRepository
44
     */
45
    protected $clientAuthenticatorRepository;
46
    /**
47
     * @var AuthorizationEndpoint
48
     */
49
    protected $authorizationEndpoint;
50
    /**
51
     * @var TokenEndpoint
52
     */
53
    protected $tokenEndpoint;
54
    /**
55
     * @var Guard
56
     */
57
    protected $guard;
58
    /**
59
     * @var Manager
60
     */
61
    protected $scopePolicyManager;
62
    /**
63
     * @var ResourceOwnerProviderInterface
64
     */
65
    protected $resourceOwnerProvider;
66
67
    /**
68
     * Server constructor.
69
     * @param ResourceOwnerProviderInterface $resourceOwnerProvider
70
     * @param null|StorageRepository $storageRepository
71
     * @param null|ConfigurationRepository $configurationRepository
72
     * @param ResponseTypeRepository $responseTypeRepository
73
     * @param null|GrantTypeRepository $grantTypeRepository
74
     * @param null|ClientAuthenticatorRepository $clientAuthenticatorRepository
75
     * @param null|Guard $guard
76
     * @param null|Manager $scopePolicyManager
77
     * @throws \Exception
78
     */
79
    public function __construct(ResourceOwnerProviderInterface $resourceOwnerProvider,
80
                                StorageRepository $storageRepository,
81
                                ConfigurationRepository $configurationRepository = null,
82
                                ?ResponseTypeRepository $responseTypeRepository = null,
83
                                ?GrantTypeRepository $grantTypeRepository = null,
84
                                ?ClientAuthenticatorRepository $clientAuthenticatorRepository = null,
85
                                ?Guard $guard = null,
86
                                ?Manager $scopePolicyManager = null)
87
    {
88
        $this->resourceOwnerProvider = $resourceOwnerProvider;
89
        $this->storageRepository = $storageRepository;
90
91
92
        if (is_null($configurationRepository)) {
93
            $this->configurationRepository = new ConfigurationRepository();
94
        } else {
95
            $this->configurationRepository = $configurationRepository;
96
        }
97
98
        if (is_null($responseTypeRepository)) {
99
            $this->responseTypeRepository = new ResponseTypeRepository(
100
                ResponseTypeRepository::getDefaultResponseTypes($this->storageRepository));
101
        } else {
102
            $this->responseTypeRepository = $responseTypeRepository;
103
        }
104
105
        if (is_null($clientAuthenticatorRepository)) {
106
            $this->clientAuthenticatorRepository = new ClientAuthenticatorRepository(
107
                ClientAuthenticatorRepository::getDefaultAuthenticators(
108
                    $this->configurationRepository, $this->storageRepository));
109
        } else {
110
            $this->clientAuthenticatorRepository = $responseTypeRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseTypeRepository can also be of type OAuth2\Repositories\ResponseTypeRepository. However, the property $clientAuthenticatorRepository is declared as type OAuth2\Repositories\ClientAuthenticatorRepository. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
111
        }
112
113
        $this->guard = $guard ? $guard : new Guard($this);
114
//        var_dump($this->guard);die;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
        $this->scopePolicyManager = $scopePolicyManager ? $scopePolicyManager : new Manager($this);
116
117
        if (is_null($grantTypeRepository)) {
118
            $this->grantTypeRepository = new GrantTypeRepository(
119
                GrantTypeRepository::getDefaultGrantTypes(
120
                    $this->configurationRepository,
121
                    $this->resourceOwnerProvider,
122
                    $this->scopePolicyManager,
123
                    $this->storageRepository));
124
        } else {
125
            $this->grantTypeRepository = $grantTypeRepository;
126
        }
127
128
        $this->authorizationEndpoint = new AuthorizationEndpoint($this);
129
        $this->tokenEndpoint = new TokenEndpoint($this);
130
    }
131
132
    /**
133
     * @return ConfigurationRepository
134
     */
135 4
    public function getConfigurationRepository(): ConfigurationRepository
136
    {
137 4
        return $this->configurationRepository;
138
    }
139
140
    /**
141
     * @return StorageRepository
142
     */
143 4
    public function getStorageRepository(): StorageRepository
144
    {
145 4
        return $this->storageRepository;
146
    }
147
148
    /**
149
     * @return ResponseTypeRepository
150
     */
151 4
    public function getResponseTypeRepository(): ResponseTypeRepository
152
    {
153 4
        return $this->responseTypeRepository;
154
    }
155
156
    /**
157
     * @return GrantTypeRepository
158
     */
159 1
    public function getGrantTypeRepository(): GrantTypeRepository
160
    {
161 1
        return $this->grantTypeRepository;
162
    }
163
164
    /**
165
     * @return ClientAuthenticatorRepository
166
     */
167 1
    public function getClientAuthenticatorRepository(): ClientAuthenticatorRepository
168
    {
169 1
        return $this->clientAuthenticatorRepository;
170
    }
171
172
    /**
173
     * @return AuthorizationEndpoint
174
     */
175 4
    public function getAuthorizationEndpoint(): AuthorizationEndpoint
176
    {
177 4
        return $this->authorizationEndpoint;
178
    }
179
180
    /**
181
     * @return TokenEndpoint
182
     */
183 1
    public function getTokenEndpoint(): TokenEndpoint
184
    {
185 1
        return $this->tokenEndpoint;
186
    }
187
188
    /**
189
     * @return Guard
190
     */
191 1
    public function getGuard(): Guard
192
    {
193 1
        return $this->guard;
194
    }
195
196
    /**
197
     * @return bool
198
     *
199
     * @see https://tools.ietf.org/html/rfc6749#section-3.1.2.1
200
     *
201
     *  Endpoint Request Confidentiality
202
     *
203
     *     The redirection endpoint SHOULD require the use of TLS as described
204
     * in Section 1.6 when the requested response type is "code" or "token",
205
     * or when the redirection request will result in the transmission of
206
     * sensitive credentials over an open network.  This specification does
207
     * not mandate the use of TLS because at the time of this writing,
208
     * requiring clients to deploy TLS is a significant hurdle for many
209
     * client developers.  If TLS is not available, the authorization server
210
     * SHOULD warn the resource owner about the insecure endpoint prior to
211
     * redirection (e.g., display a message during the authorization
212
     * request).
213
     *
214
     * Lack of transport-layer security can have a severe impact on the
215
     * security of the client and the protected resources it is authorized
216
     * to access.  The use of transport-layer security is particularly
217
     * critical when the authorization process is used as a form of
218
     * delegated end-user authentication by the client (e.g., third-party
219
     * sign-in service).
220
     */
221 1
    public function isSecure()
222
    {
223 1
        return isset($_SERVER['HTTPS']) && ('on' == strtolower($_SERVER['HTTPS']) || 1 == $_SERVER['HTTPS']);
224
    }
225
226
    /**
227
     * @return Manager
228
     */
229 3
    public function getScopePolicyManager(): Manager
230
    {
231 3
        return $this->scopePolicyManager;
232
    }
233
234
    /**
235
     * @return ResourceOwnerProviderInterface
236
     */
237
    public function getResourceOwnerProvider(): ResourceOwnerProviderInterface
238
    {
239
        return $this->resourceOwnerProvider;
240
    }
241
}