Completed
Pull Request — master (#14)
by William
02:24
created

AppleAccessToken::__construct()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12.9147

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 22
cts 27
cp 0.8148
rs 6.9666
c 0
b 0
f 0
cc 12
nc 40
nop 1
crap 12.9147

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 3
    public function __construct(array $options = [])
36
    {
37 3
        if (array_key_exists('refresh_token', $options)) {
38 2
            if (empty($options['id_token'])) {
39
                throw new InvalidArgumentException('Required option not passed: "id_token"');
40
            }
41
42 2
            $decoded = null;
43 2
            $keys = $this->getAppleKey();
44 2
            $last = end($keys);
45 2
            foreach ($keys as $key) {
46
                try {
47 2
                    $decoded = JWT::decode($options['id_token'], $key, ['RS256']);
48 1
                    break;
49 1
                } catch (\Exception $exception) {
50 1
                    if ($last === $key) {
51 1
                        throw $exception;
52
                    }
53
                }
54
            }
55 1
            if (null === $decoded) {
56
                throw new \Exception('Got no data within "id_token"!');
57
            }
58 1
            $payload = json_decode(json_encode($decoded), true);
59
60 1
            $options['resource_owner_id'] = $payload['sub'];
61
62 1
            if (isset($payload['email_verified']) && $payload['email_verified']) {
63
                $options['email'] = $payload['email'];
64
            }
65
66 1
            if (isset($payload['is_private_email'])) {
67
                $this->isPrivateEmail = $payload['is_private_email'];
68
            }
69
        }
70
71 2
        parent::__construct($options);
72
73 2
        if (isset($options['id_token'])) {
74 1
            $this->idToken = $options['id_token'];
75
        }
76
77 2
        if (isset($options['email'])) {
78
            $this->email = $options['email'];
79
        }
80 2
    }
81
82
    /**
83
     * @return array Apple's JSON Web Key
84
     */
85 2
    protected function getAppleKey()
86
    {
87 2
        $client = new \GuzzleHttp\Client();
88 2
        $request = $client->get('https://appleid.apple.com/auth/keys');
89 2
        $response = $request->getBody();
90
91 2
        if ($response) {
92 2
            return JWK::parseKeySet(json_decode($response, true));
93
        }
94
        return false;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 1
    public function getIdToken()
101
    {
102 1
        return $this->idToken;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getEmail()
109
    {
110
        return $this->email;
111
    }
112
113
    /**
114
     * @return boolean
115
     */
116
    public function isPrivateEmail()
117
    {
118
        return $this->isPrivateEmail;
119
    }
120
}
121