Passed
Push — master ( 3948f8...d3c224 )
by Dāvis
07:09
created

GoogleUser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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\SocialUserInterface;
7
8
class GoogleUser implements ResourceOwnerInterface, SocialUserInterface
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
     * @var bool
42
     */
43
    protected $returnsEmail = true;
44
45
    /**
46
     * @return bool
47
     */
48
    public function returnsEmail()
49
    {
50
        return $this->returnsEmail;
51
    }
52
53
    /**
54
     * @param array $response
55
     */
56
    public function __construct(array $response)
57
    {
58
        $this->response = $response;
59
60
        $this->id = (int)$this->response['id'];
61
62
        if (!empty($this->response['emails'])) {
63
            $this->email = $this->response['emails'][0]['value'];
64
        }
65
66
        $this->firstName = $this->response['name']['givenName'];
67
68
        $this->lastName = $this->response['name']['familyName'];
69
70
        $username = explode('@', $this->email);
71
        $username = preg_replace('/[^a-z\d]/i', '', $username[0]);
72
        $this->username = $username;
73
    }
74
75
    /**
76
     * Get preferred display name.
77
     *
78
     * @return string
79
     */
80
    public function getName()
81
    {
82
        return $this->response['displayName'];
83
    }
84
85
    /**
86
     * Get avatar image URL.
87
     *
88
     * @return string|null
89
     */
90
    public function getAvatar()
91
    {
92
        if (!empty($this->response['image']['url'])) {
93
            return $this->response['image']['url'];
94
        }
95
96
        return '';
97
    }
98
99
    /**
100
     * Get user data as an array.
101
     *
102
     * @return array
103
     */
104
    public function toArray()
105
    {
106
        return $this->response;
107
    }
108
109
    /**
110
     * Get the value of Id
111
     *
112
     * @return integer
113
     */
114
    public function getId()
115
    {
116
        return $this->id;
117
    }
118
119
    /**
120
     * Get the value of Email
121
     *
122
     * @return string
123
     */
124
    public function getEmail()
125
    {
126
        return $this->email;
127
    }
128
129
    /**
130
     * Get the value of First Name
131
     *
132
     * @return string
133
     */
134
    public function getFirstName()
135
    {
136
        return $this->firstName;
137
    }
138
139
    /**
140
     * Get the value of Last Name
141
     *
142
     * @return string
143
     */
144
    public function getLastName()
145
    {
146
        return $this->lastName;
147
    }
148
149
    /**
150
     * Get the value of Username
151
     *
152
     * @return string
153
     */
154
    public function getUsername()
155
    {
156
        return $this->username;
157
    }
158
}
159