Passed
Branch master (a0cc06)
by Dāvis
17:06
created

FacebookUser::getField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
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\SocialUser;
7
8
class FacebookUser 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
        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
    /**
175
     * Returns the lower bound of the user's age range
176
     *
177
     * @return integer|null
178
     */
179 View Code Duplication
    public function getMinAge()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
    {
181
        if (isset($this->response['age_range']['min'])) {
182
            return $this->response['age_range']['min'];
183
        }
184
185
        return null;
186
    }
187
188
    /**
189
     * Returns the upper bound of the user's age range
190
     *
191
     * @return integer|null
192
     */
193 View Code Duplication
    public function getMaxAge()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        if (isset($this->response['age_range']['max'])) {
196
            return $this->response['age_range']['max'];
197
        }
198
199
        return null;
200
    }
201
202
    /**
203
     * Returns all the data obtained about the user.
204
     *
205
     * @return array
206
     */
207
    public function toArray()
208
    {
209
        return $this->response;
210
    }
211
212
    /**
213
     * Returns a field from the Graph node data.
214
     *
215
     * @param string $key
216
     *
217
     * @return mixed|null
218
     */
219
    private function getField($key)
220
    {
221
        return isset($this->response[$key]) ? $this->response[$key] : null;
222
    }
223
224
    /**
225
     * Get the value of Id
226
     *
227
     * @return integer
228
     */
229
    public function getId()
230
    {
231
        return $this->id;
232
    }
233
234
    /**
235
     * Get the value of Email
236
     *
237
     * @return string
238
     */
239
    public function getEmail()
240
    {
241
        return $this->email;
242
    }
243
244
    /**
245
     * Get the value of First Name
246
     *
247
     * @return string
248
     */
249
    public function getFirstName()
250
    {
251
        return $this->firstName;
252
    }
253
254
    /**
255
     * Get the value of Last Name
256
     *
257
     * @return string
258
     */
259
    public function getLastName()
260
    {
261
        return $this->lastName;
262
    }
263
264
    /**
265
     * Get the value of Username
266
     *
267
     * @return string
268
     */
269
    public function getUsername()
270
    {
271
        return $this->username;
272
    }
273
274
}
275