Completed
Push — master ( 413d7d...5afdfd )
by Patrick
02:46
created

AppleAccessToken::__construct()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 24.6381

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 15
cts 27
cp 0.5556
rs 6.9666
c 0
b 0
f 0
cc 12
nc 40
nop 1
crap 24.6381

How to fix   Complexity   

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:

1
<?php
2
3
namespace League\OAuth2\Client\Token;
4
5
use Firebase\JWT\JWK;
6
use Firebase\JWT\JWT;
7
use InvalidArgumentException;
8
9
class AppleAccessToken extends AccessToken
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $idToken;
15
16
    /**
17
     * @var string
18
     */
19
    protected $email;
20
21
    /**
22
     * @var boolean
23
     */
24
    protected $isPrivateEmail;
25
26
    /**
27
     * Constructs an access token.
28
     *
29
     * @param array $options An array of options returned by the service provider
30
     *     in the access token request. The `access_token` option is required.
31
     * @throws InvalidArgumentException if `access_token` is not provided in `$options`.
32
     *
33
     * @throws \Exception
34
     */
35 2
    public function __construct(array $options = [])
36
    {
37 2
        if (array_key_exists('refresh_token', $options))
38
        {
39 1
            if (empty($options['id_token'])) {
40
                throw new InvalidArgumentException('Required option not passed: "id_token"');
41
            }
42
43 1
            $decoded = null;
44 1
            $keys = $this->getAppleKey();
45 1
            $last = end($keys);
46 1
            foreach ($keys as $key) {
47
                try {
48 1
                    $decoded = JWT::decode($options['id_token'], $key, ['RS256']);
49
                    break;
50 1
                } catch (\Exception $exception) {
51 1
                    if ($last === $key) {
52 1
                        throw $exception;
53
                    }
54
                }
55
            }
56
            if (null === $decoded) {
57
                throw new \Exception('Got no data within "id_token"!');
58
            }
59
            $payload = json_decode(json_encode($decoded), true);
60
61
            $options['resource_owner_id'] = $payload['sub'];
62
63
            if (isset($payload['email_verified']) && $payload['email_verified']) {
64
                $options['email'] = $payload['email'];
65
            }
66
67
            if (isset($payload['is_private_email'])) {
68
                $this->isPrivateEmail = $payload['is_private_email'];
69
            }
70
        }
71
72 1
        parent::__construct($options);
73
74 1
        if (isset($options['id_token'])) {
75
            $this->idToken = $options['id_token'];
76
        }
77
78 1
        if (isset($options['email'])) {
79
            $this->email = $options['email'];
80
        }
81 1
    }
82
83
    /**
84
     * @return array Apple's JSON Web Key
85
     */
86 1
    protected function getAppleKey()
87
    {
88 1
        return JWK::parseKeySet(json_decode(file_get_contents('https://appleid.apple.com/auth/keys'), true));
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getIdToken()
95
    {
96
        return $this->idToken;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getEmail()
103
    {
104
        return $this->email;
105
    }
106
107
    /**
108
     * @return boolean
109
     */
110
    public function isPrivateEmail()
111
    {
112
        return $this->isPrivateEmail;
113
    }
114
}
115