Completed
Push — master ( 89be15...80fd60 )
by Igor
11:15
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\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