Completed
Push — master ( 89be15...80fd60 )
by Igor
11:15
created

Facebook::tokenData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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