Completed
Push — master ( d6ee99...bac4fd )
by Igor
01:38
created

Facebook   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A email() 0 4 1
A tokenData() 0 9 1
A profileData() 0 17 3
1
<?php
2
3
namespace app\modules\auth\oauth\parsers;
4
5
use yii\helpers\ArrayHelper;
6
use app\modules\auth\oauth\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
            'photo' => $photo
62
        ];
63
    }
64
}
65