Completed
Push — master ( d47dcb...df81e1 )
by Igor
03:40
created

Facebook::profileData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 12
nc 2
nop 0
1
<?php
2
3
namespace app\parsers\auth;
4
5
use yii\helpers\ArrayHelper;
6
use app\parsers\auth\Parser;
7
8
/**
9
 * Parser for Facebook OAuth
10
 *
11
 * @property ClientInterface $client
12
 * @property array $profile
13
 * @property array $token
14
 */
15
class Facebook extends Parser
16
{
17
    /**
18
     * Get email
19
     *
20
     * @return string|null
21
     */
22
    public function email(): ?string
23
    {
24
        return ArrayHelper::getValue($this->profile, 'email');
25
    }
26
27
    /**
28
     * Get token info
29
     *
30
     * @return array
31
     */
32
    public function tokenData(): array
33
    {
34
        return [
35
            'profile_id' => ArrayHelper::getValue($this->profile, 'id'),
36
            'profile_url' => ArrayHelper::getValue($this->profile, 'link'),
37
            'access_token' => ArrayHelper::getValue($this->token, 'access_token'),
38
            'access_token_secret' => ''
39
        ];
40
    }
41
42
    /**
43
     * Get profile info
44
     *
45
     * @return array
46
     */
47
    public function profileData(): array
48
    {
49
        $profileId = ArrayHelper::getValue($this->profile, 'id');
50
        $photoUrl = 'https://graph.facebook.com/' . $profileId . '/picture?width=500&redirect=false';
51
        $photoRes = json_decode(file_get_contents($photoUrl));
52
53
        if (is_object($photoRes) && isset($photoRes->data)) {
54
            $photo = $photoRes->data->url;
55
        } else {
56
            $photo = ArrayHelper::getValue($this->profile, 'picture.data.url', '');
57
        }
58
59
        return [
60
            'full_name' => trim(ArrayHelper::getValue($this->profile, 'name')),
61
            'birth_day' => '',
62
            'photo' => $photo
63
        ];
64
    }
65
}
66