Passed
Branch master (c65ffc)
by Dāvis
03:08
created

GoogleUser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvatar() 0 4 2
A toArray() 0 3 1
A __construct() 0 17 2
A getLastName() 0 3 1
A getId() 0 3 1
A getUsername() 0 3 1
A getFirstName() 0 3 1
A getName() 0 3 1
A getEmail() 0 3 1
1
<?php
2
3
namespace Sludio\HelperBundle\Oauth\Client\Provider\Google;
4
5
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
6
use Sludio\HelperBundle\Oauth\Component\SocialUser;
7
8
class GoogleUser implements ResourceOwnerInterface, SocialUser
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $response;
14
15
    /**
16
     * @var integer
17
     */
18
    protected $id;
19
20
    /**
21
     * @var string
22
     */
23
    protected $email;
24
25
    /**
26
     * @var string
27
     */
28
    protected $firstName;
29
30
    /**
31
     * @var string
32
     */
33
    protected $lastName;
34
35
    /**
36
     * @var string
37
     */
38
    protected $username;
39
40
    /**
41
     * @param array $response
42
     */
43
    public function __construct(array $response)
44
    {
45
        $this->response = $response;
46
47
        $this->id = intval($this->response['id']);
48
49
        if (!empty($this->response['emails'])) {
50
            $this->email = $this->response['emails'][0]['value'];
51
        }
52
53
        $this->firstName = $this->response['name']['givenName'];
54
55
        $this->lastName = $this->response['name']['familyName'];
56
57
        $username = explode('@', $this->email);
58
        $username = preg_replace('/[^a-z\d]/i', '', $username[0]);
59
        $this->username = $username;
60
    }
61
62
    /**
63
     * Get preferred display name.
64
     *
65
     * @return string
66
     */
67
    public function getName()
68
    {
69
        return $this->response['displayName'];
70
    }
71
72
    /**
73
     * Get avatar image URL.
74
     *
75
     * @return string|null
76
     */
77
    public function getAvatar()
78
    {
79
        if (!empty($this->response['image']['url'])) {
80
            return $this->response['image']['url'];
81
        }
82
    }
83
84
    /**
85
     * Get user data as an array.
86
     *
87
     * @return array
88
     */
89
    public function toArray()
90
    {
91
        return $this->response;
92
    }
93
94
    /**
95
     * Get the value of Id
96
     *
97
     * @return integer
98
     */
99
    public function getId()
100
    {
101
        return $this->id;
102
    }
103
104
    /**
105
     * Get the value of Email
106
     *
107
     * @return string
108
     */
109
    public function getEmail()
110
    {
111
        return $this->email;
112
    }
113
114
    /**
115
     * Get the value of First Name
116
     *
117
     * @return string
118
     */
119
    public function getFirstName()
120
    {
121
        return $this->firstName;
122
    }
123
124
    /**
125
     * Get the value of Last Name
126
     *
127
     * @return string
128
     */
129
    public function getLastName()
130
    {
131
        return $this->lastName;
132
    }
133
134
    /**
135
     * Get the value of Username
136
     *
137
     * @return string
138
     */
139
    public function getUsername()
140
    {
141
        return $this->username;
142
    }
143
144
}
145