GoogleUser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 146
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

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