Passed
Push — master ( 8eea92...da79af )
by Dāvis
03:03
created

FacebookUser::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 8
nop 1
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Oauth\Client\Provider\Facebook;
4
5
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
6
use Sludio\HelperBundle\Oauth\Component\SocialUserInterface;
7
8
class FacebookUser 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
     * @param  array $response
42
     */
43
    public function __construct(array $response)
44
    {
45
        $this->response = $response;
46
47
        if (!empty($response['picture']['data']['url'])) {
48
            $this->response['picture_url'] = $response['picture']['data']['url'];
49
        }
50
51
        if (isset($response['picture']['data']['is_silhouette'])) {
52
            $this->response['is_silhouette'] = $response['picture']['data']['is_silhouette'];
53
        }
54
55
        if (!empty($response['cover']['source'])) {
56
            $this->response['cover_photo_url'] = $response['cover']['source'];
57
        }
58
59
        $this->id = intval($this->getField('id'));
60
61
        $this->email = $this->getField('email');
62
63
        $this->firstName = $this->getField('first_name');
64
65
        $this->lastName = $this->getField('last_name');
66
67
        $username = explode('@', $this->email);
68
        $username = preg_replace('/[^a-z\d]/i', '', $username[0]);
69
        $this->username = $username;
70
    }
71
72
    /**
73
     * Returns the name for the user as a string if present.
74
     *
75
     * @return string|null
76
     */
77
    public function getName()
78
    {
79
        return $this->getField('name');
80
    }
81
82
    /**
83
     * Returns the current location of the user as an array.
84
     *
85
     * @return array|null
86
     */
87
    public function getHometown()
88
    {
89
        return $this->getField('hometown');
90
    }
91
92
    /**
93
     * Returns the "about me" bio for the user as a string if present.
94
     *
95
     * @return string|null
96
     * @deprecated The bio field was removed in Graph v2.8
97
     */
98
    public function getBio()
99
    {
100
        return $this->getField('bio');
101
    }
102
103
    /**
104
     * Returns if user has not defined a specific avatar
105
     *
106
     * @return boolean
107
     */
108
109
    public function isDefaultPicture()
110
    {
111
        return $this->getField('is_silhouette');
112
    }
113
114
    /**
115
     * Returns the profile picture of the user as a string if present.
116
     *
117
     * @return string|null
118
     */
119
    public function getPictureUrl()
120
    {
121
        return $this->getField('picture_url');
122
    }
123
124
    /**
125
     * Returns the cover photo URL of the user as a string if present.
126
     *
127
     * @return string|null
128
     */
129
    public function getCoverPhotoUrl()
130
    {
131
        return $this->getField('cover_photo_url');
132
    }
133
134
    /**
135
     * Returns the gender for the user as a string if present.
136
     *
137
     * @return string|null
138
     */
139
    public function getGender()
140
    {
141
        return $this->getField('gender');
142
    }
143
144
    /**
145
     * Returns the locale of the user as a string if available.
146
     *
147
     * @return string|null
148
     */
149
    public function getLocale()
150
    {
151
        return $this->getField('locale');
152
    }
153
154
    /**
155
     * Returns the Facebook URL for the user as a string if available.
156
     *
157
     * @return string|null
158
     */
159
    public function getLink()
160
    {
161
        return $this->getField('link');
162
    }
163
164
    /**
165
     * Returns the current timezone offset from UTC (from -24 to 24)
166
     *
167
     * @return float|null
168
     */
169
    public function getTimezone()
170
    {
171
        return $this->getField('timezone');
172
    }
173
174
    public function getAgeRange()
175
    {
176
        return $this->getField('age_range');
177
    }
178
179
    /**
180
     * Returns all the data obtained about the user.
181
     *
182
     * @return array
183
     */
184
    public function toArray()
185
    {
186
        return $this->response;
187
    }
188
189
    /**
190
     * Returns a field from the Graph node data.
191
     *
192
     * @param string $key
193
     *
194
     * @return mixed|null
195
     */
196
    private function getField($key)
197
    {
198
        return isset($this->response[$key]) ? $this->response[$key] : null;
199
    }
200
201
    /**
202
     * Get the value of Id
203
     *
204
     * @return integer
205
     */
206
    public function getId()
207
    {
208
        return $this->id;
209
    }
210
211
    /**
212
     * Get the value of Email
213
     *
214
     * @return string
215
     */
216
    public function getEmail()
217
    {
218
        return $this->email;
219
    }
220
221
    /**
222
     * Get the value of First Name
223
     *
224
     * @return string
225
     */
226
    public function getFirstName()
227
    {
228
        return $this->firstName;
229
    }
230
231
    /**
232
     * Get the value of Last Name
233
     *
234
     * @return string
235
     */
236
    public function getLastName()
237
    {
238
        return $this->lastName;
239
    }
240
241
    /**
242
     * Get the value of Username
243
     *
244
     * @return string
245
     */
246
    public function getUsername()
247
    {
248
        return $this->username;
249
    }
250
251
}
252