Completed
Push — master ( 07f7e6...c0faed )
by Woody
9s
created

GoogleUser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 121
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getId() 0 7 2
A getName() 0 7 3
A getFirstName() 0 7 2
A getLastName() 0 7 2
A getEmail() 0 10 3
A getHostedDomain() 0 11 3
A getAvatar() 0 10 3
A toArray() 0 4 1
1
<?php
2
3
namespace League\OAuth2\Client\Provider;
4
5
class GoogleUser implements ResourceOwnerInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $response;
11
12
    /**
13
     * @param array $response
14
     */
15
    public function __construct(array $response)
16
    {
17
        $this->response = $response;
18
    }
19
20
    public function getId()
21
    {
22
        if (array_key_exists('sub', $this->response)) {
23
            return $this->response['sub'];
24
        }
25
        return $this->response['id'];
26
    }
27
28
    /**
29
     * Get preferred display name.
30
     *
31
     * @return string
32
     */
33
    public function getName()
34
    {
35
        if (array_key_exists('name', $this->response) && is_string($this->response['name'])) {
36
            return $this->response['name'];
37
        }
38
        return $this->response['displayName'];
39
    }
40
41
    /**
42
     * Get preferred first name.
43
     *
44
     * @return string
45
     */
46
    public function getFirstName()
47
    {
48
        if (array_key_exists('given_name', $this->response)) {
49
            return $this->response['given_name'];
50
        }
51
        return $this->response['name']['givenName'];
52
    }
53
54
    /**
55
     * Get preferred last name.
56
     *
57
     * @return string
58
     */
59
    public function getLastName()
60
    {
61
        if (array_key_exists('family_name', $this->response)) {
62
            return $this->response['family_name'];
63
        }
64
        return $this->response['name']['familyName'];
65
    }
66
67
    /**
68
     * Get email address.
69
     *
70
     * @return string|null
71
     */
72
    public function getEmail()
73
    {
74
        if (array_key_exists('email', $this->response)) {
75
            return $this->response['email'];
76
        }
77
        if (!empty($this->response['emails'])) {
78
            return $this->response['emails'][0]['value'];
79
        }
80
        return null;
81
    }
82
83
    /**
84
     * Get hosted domain.
85
     *
86
     * @return string|null
87
     */
88
    public function getHostedDomain()
89
    {
90
        if (array_key_exists('hd', $this->response)) {
91
            return $this->response['hd'];
92
        }
93
        if (array_key_exists('domain', $this->response)) {
94
            return $this->response['domain'];
95
        }
96
97
        return null;
98
    }
99
100
    /**
101
     * Get avatar image URL.
102
     *
103
     * @return string|null
104
     */
105
    public function getAvatar()
106
    {
107
        if (array_key_exists('picture', $this->response)) {
108
            return $this->response['picture'];
109
        }
110
        if (!empty($this->response['image']['url'])) {
111
            return $this->response['image']['url'];
112
        }
113
        return null;
114
    }
115
116
    /**
117
     * Get user data as an array.
118
     *
119
     * @return array
120
     */
121
    public function toArray()
122
    {
123
        return $this->response;
124
    }
125
}
126