GrantTypeUtils   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 5
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureRequestedGrantTypeIsSupported() 0 7 2
A ensureInputDataAreValid() 0 16 3
1
<?php
2
/**
3
 * @author Boris Guéry <[email protected]>
4
 */
5
6
namespace Bgy\OAuth2\Utils;
7
8
use Bgy\OAuth2\GrantType\GrantType;
9
use Bgy\OAuth2\GrantType\MissingOrInvalidInputData;
10
use Bgy\OAuth2\GrantType\UnsupportedGrantType;
11
use Bgy\OAuth2\TokenRequestAttempt;
12
13
class GrantTypeUtils
14
{
15
    public static function ensureRequestedGrantTypeIsSupported(GrantType $grantType, TokenRequestAttempt $tokenRequestAttempt)
16
    {
17
        if (strtolower($grantType->getIdentifier()) !== strtolower($tokenRequestAttempt->getGrantType())) {
18
19
            throw new UnsupportedGrantType(self::class, $grantType->getIdentifier(), $tokenRequestAttempt->getGrantType());
20
        }
21
    }
22
23
    public static function ensureInputDataAreValid(GrantType $grantType, TokenRequestAttempt $tokenRequestAttempt)
24
    {
25
        $inputDataKeys = $tokenRequestAttempt->getInputData()->all();
26
27
        // check if we got the actual input data or just its keys
28
        if (array_keys($tokenRequestAttempt->getInputData()->all()) !== range(0, count($tokenRequestAttempt->getInputData()->all()) - 1)) {
29
30
            $inputDataKeys = array_keys($tokenRequestAttempt->getInputData()->all());
31
        }
32
33
        // using != instead of !== does not check the order of the properties
34
        if ($grantType->getRequiredInputData() != array_values(array_intersect($inputDataKeys, $grantType->getRequiredInputData()))) {
35
36
            throw new MissingOrInvalidInputData($grantType->getIdentifier(), $inputDataKeys, $grantType->getRequiredInputData());
37
        }
38
    }
39
}
40