Completed
Push — master ( 046422...8eb4ac )
by ARCANEDEV
08:13 queued 14s
created

FacebookProvider::getCodeFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php namespace Arcanedev\Socialite\OAuth\Two;
2
3
use Arcanedev\Socialite\Base\OAuthTwoProvider;
4
5
/**
6
 * Class     FacebookProvider
7
 *
8
 * @package  Arcanedev\Socialite\OAuth\Two
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class FacebookProvider extends OAuthTwoProvider
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * The base Facebook Graph URL.
19
     *
20
     * @var string
21
     */
22
    protected $graphUrl = 'https://graph.facebook.com';
23
24
    /**
25
     * The Graph API version for the request.
26
     *
27
     * @var string
28
     */
29
    protected $version = 'v2.5';
30
31
    /**
32
     * The user fields being requested.
33
     *
34
     * @var array
35
     */
36
    protected $fields = ['name', 'email', 'gender', 'verified'];
37
38
    /**
39
     * The scopes being requested.
40
     *
41
     * @var array
42
     */
43
    protected $scopes = ['email'];
44
45
    /**
46
     * Display the dialog in a popup view.
47
     *
48
     * @var bool
49
     */
50
    protected $popup = false;
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Main Functions
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function getAuthUrl($state)
60
    {
61
        return $this->buildAuthUrlFromBase(
62
            "https://www.facebook.com/{$this->version}/dialog/oauth", $state
63
        );
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getTokenUrl()
70
    {
71
        return "{$this->graphUrl}/oauth/access_token";
72
    }
73
74
    /**
75
     * Get the access token for the given code.
76
     *
77
     * @param  string  $code
78
     *
79
     * @return string
80
     */
81
    public function getAccessToken($code)
82
    {
83
        $response = $this->getHttpClient()->get($this->getTokenUrl(), [
84
            'query' => $this->getTokenFields($code),
85
        ]);
86
87
        return $this->parseAccessToken($response->getBody());
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function parseAccessToken($body)
94
    {
95
        parse_str($body);
96
97
        return $access_token;
0 ignored issues
show
Bug introduced by
The variable $access_token does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function getUserByToken($token)
104
    {
105
        $appSecretProof = hash_hmac('sha256', $token, $this->clientSecret);
106
        $response       = $this->getHttpClient()->get("{$this->graphUrl}/{$this->version}/me?access_token={$token}&appsecret_proof={$appSecretProof}&fields=" . implode(',', $this->fields), [
107
            'headers' => [
108
                'Accept' => 'application/json',
109
            ],
110
        ]);
111
112
        return json_decode($response->getBody(), true);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function mapUserToObject(array $user)
119
    {
120
        $avatarUrl = "{$this->graphUrl}/{$this->version}/{$user['id']}/picture";
121
122
        return (new User)->setRaw($user)->map([
123
            'id'              => $user['id'],
124
            'nickname'        => null,
125
            'name'            => isset($user['name']) ? $user['name'] : null,
126
            'email'           => isset($user['email']) ? $user['email'] : null,
127
            'avatar'          => $avatarUrl . '?type=normal',
128
            'avatar_original' => $avatarUrl . '?width=1920',
129
        ]);
130
    }
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getCodeFields($state = null)
135
    {
136
        $fields = parent::getCodeFields($state);
137
138
        if ($this->popup) {
139
            $fields['display'] = 'popup';
140
        }
141
142
        return $fields;
143
    }
144
145
    /**
146
     * Set the user fields to request from Facebook.
147
     *
148
     * @param  array  $fields
149
     *
150
     * @return self
151
     */
152
    public function fields(array $fields)
153
    {
154
        $this->fields = $fields;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Set the dialog to be displayed as a popup.
161
     *
162
     * @return self
163
     */
164
    public function asPopup()
165
    {
166
        $this->popup = true;
167
168
        return $this;
169
    }
170
}
171