Passed
Branch master (c65ffc)
by Dāvis
03:08
created

Facebook::getAccessToken()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Oauth\Client\Provider\Facebook;
4
5
use InvalidArgumentException;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use Psr\Http\Message\ResponseInterface;
9
use Sludio\HelperBundle\Oauth\Client\Provider\BaseProvider;
10
use Sludio\HelperBundle\Oauth\Exception\FacebookProviderException;
11
12
class Facebook extends BaseProvider
13
{
14
    /**
15
     * Production Graph API URL.
16
     *
17
     * @const string
18
     */
19
    const BASE_FACEBOOK_URL = 'https://www.facebook.com/';
20
21
    /**
22
     * Production Graph API URL.
23
     *
24
     * @const string
25
     */
26
    const BASE_GRAPH_URL = 'https://graph.facebook.com/';
27
28
    /**
29
     * Regular expression used to check for graph API version format
30
     *
31
     * @const string
32
     */
33
    const GRAPH_API_VERSION_REGEX = '~^v\d+\.\d+$~';
34
35
    /**
36
     * The Graph API version to use for requests.
37
     *
38
     * @var string
39
     */
40
    protected $graphApiVersion;
41
42
    /**
43
     * @param array $options
44
     * @param array $collaborators
45
     *
46
     * @throws \InvalidArgumentException
47
     */
48
    public function __construct($options = [], array $collaborators = [], $generator = null)
49
    {
50
        parent::__construct($options, $collaborators, $generator);
51
52
        if (empty($options['graphApiVersion'])) {
53
            throw new InvalidArgumentException('error_facebook_graph_api_version_not_set');
54
        } elseif (!preg_match(self::GRAPH_API_VERSION_REGEX, $options['graphApiVersion'])) {
55
            throw new InvalidArgumentException('error_facebook_wrong_graph_api_version');
56
        }
57
58
        $this->graphApiVersion = $options['graphApiVersion'];
59
    }
60
61
    public function getBaseAuthorizationUrl()
62
    {
63
        return $this->getBaseFacebookUrl().$this->graphApiVersion.'/dialog/oauth';
64
    }
65
66
    public function getBaseAccessTokenUrl(array $params)
67
    {
68
        return $this->getBaseGraphUrl().$this->graphApiVersion.'/oauth/access_token';
69
    }
70
71
    public function getDefaultScopes()
72
    {
73
        return [
74
            'public_profile',
75
            'email',
76
        ];
77
    }
78
79
    public function getResourceOwnerDetailsUrl(AccessToken $token)
80
    {
81
        $fields = [
82
            'id',
83
            'name',
84
            'first_name',
85
            'last_name',
86
            'email',
87
            'hometown',
88
            'picture.type(large){url,is_silhouette}',
89
            'cover{source}',
90
            'gender',
91
            'locale',
92
            'link',
93
            'timezone',
94
            'age_range',
95
        ];
96
97
        // backwards compatibility with less than v2.8
98
        if ((float)substr($this->graphApiVersion, 1) < 2.8) {
99
            $fields[] = 'bio';
100
        }
101
102
        $appSecretProof = AppSecretProof::create($this->clientSecret, $token->getToken());
103
104
        return $this->getBaseGraphUrl().$this->graphApiVersion.'/me?fields='.implode(',', $fields).'&access_token='.$token.'&appsecret_proof='.$appSecretProof;
105
    }
106
107
    public function getAccessToken($grant = 'authorization_code', array $params = [], array $attributes = [])
108
    {
109
        if (isset($params['refresh_token'])) {
110
            throw new FacebookProviderException('error_facebook_token_refresh_not_supported');
111
        }
112
113
        return parent::getAccessToken($grant, $params, $attributes);
114
    }
115
116
    public function getLongLivedAccessToken($accessToken)
117
    {
118
        $params = [
119
            'fb_exchange_token' => (string)$accessToken,
120
        ];
121
122
        return $this->getAccessToken('fb_exchange_token', $params);
123
    }
124
125
    protected function createResourceOwner(array $response, AccessToken $token)
126
    {
127
        return new FacebookUser($response);
128
    }
129
130
    protected function checkResponse(ResponseInterface $response, $data)
131
    {
132
        if (!empty($data['error'])) {
133
            throw new IdentityProviderException('error_facebook_bad_response', 400, $response);
0 ignored issues
show
Bug introduced by
$response of type Psr\Http\Message\ResponseInterface is incompatible with the type string|array expected by parameter $response of League\OAuth2\Client\Pro...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            throw new IdentityProviderException('error_facebook_bad_response', 400, /** @scrutinizer ignore-type */ $response);
Loading history...
134
        }
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    protected function getContentType(ResponseInterface $response)
141
    {
142
        $type = parent::getContentType($response);
143
144
        // Fix for Facebook's pseudo-JSONP support
145
        if (strpos($type, 'javascript') !== false) {
146
            return 'application/json';
147
        }
148
149
        // Fix for Facebook's pseudo-urlencoded support
150
        if (strpos($type, 'plain') !== false) {
151
            return 'application/x-www-form-urlencoded';
152
        }
153
154
        return $type;
155
    }
156
157
    /**
158
     * Get the base Facebook URL.
159
     *
160
     * @return string
161
     */
162
    private function getBaseFacebookUrl()
163
    {
164
        return static::BASE_FACEBOOK_URL;
165
    }
166
167
    /**
168
     * Get the base Graph API URL.
169
     *
170
     * @return string
171
     */
172
    private function getBaseGraphUrl()
173
    {
174
        return static::BASE_GRAPH_URL;
175
    }
176
}
177