Facebook   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 13
Bugs 0 Features 1
Metric Value
wmc 16
eloc 122
dl 0
loc 206
c 13
b 0
f 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDialogUri() 0 10 2
A __construct() 0 12 2
A getAccessTokenEndpoint() 0 3 1
A getApiVersionString() 0 3 2
B parseAccessTokenResponse() 0 32 7
A getAuthorizationEndpoint() 0 3 1
A getScopesDelimiter() 0 3 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Exception\Exception;
7
use OAuth\Common\Http\Client\ClientInterface;
8
use OAuth\Common\Http\Exception\TokenResponseException;
9
use OAuth\Common\Http\Uri\Uri;
10
use OAuth\Common\Http\Uri\UriInterface;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
14
class Facebook extends AbstractService
15
{
16
    /**
17
     * Facebook www url - used to build dialog urls.
18
     */
19
    const WWW_URL = 'https://www.facebook.com/';
20
21
    /**
22
     * Defined scopes.
23
     *
24
     * If you don't think this is scary you should not be allowed on the web at all
25
     *
26
     * @see https://developers.facebook.com/docs/reference/login/
27
     * @see https://developers.facebook.com/tools/explorer For a list of permissions use 'Get Access Token'
28
     */
29
    // Default scope
30
    const SCOPE_PUBLIC_PROFILE = 'public_profile';
31
    // Email scopes
32
    const SCOPE_EMAIL = 'email';
33
    // Extended permissions
34
    const SCOPE_READ_FRIENDLIST = 'read_friendlists';
35
    const SCOPE_READ_INSIGHTS = 'read_insights';
36
    const SCOPE_READ_MAILBOX = 'read_mailbox';
37
    const SCOPE_READ_PAGE_MAILBOXES = 'read_page_mailboxes';
38
    const SCOPE_READ_REQUESTS = 'read_requests';
39
    const SCOPE_READ_STREAM = 'read_stream';
40
    const SCOPE_VIDEO_UPLOAD = 'video_upload';
41
    const SCOPE_XMPP_LOGIN = 'xmpp_login';
42
    const SCOPE_USER_ONLINE_PRESENCE = 'user_online_presence';
43
    const SCOPE_FRIENDS_ONLINE_PRESENCE = 'friends_online_presence';
44
    const SCOPE_ADS_MANAGEMENT = 'ads_management';
45
    const SCOPE_ADS_READ = 'ads_read';
46
    const SCOPE_CREATE_EVENT = 'create_event';
47
    const SCOPE_CREATE_NOTE = 'create_note';
48
    const SCOPE_EXPORT_STREAM = 'export_stream';
49
    const SCOPE_MANAGE_FRIENDLIST = 'manage_friendlists';
50
    const SCOPE_MANAGE_NOTIFICATIONS = 'manage_notifications';
51
    const SCOPE_PHOTO_UPLOAD = 'photo_upload';
52
    const SCOPE_PUBLISH_ACTIONS = 'publish_actions';
53
    const SCOPE_PUBLISH_CHECKINS = 'publish_checkins';
54
    const SCOPE_PUBLISH_STREAM = 'publish_stream';
55
    const SCOPE_RSVP_EVENT = 'rsvp_event';
56
    const SCOPE_SHARE_ITEM = 'share_item';
57
    const SCOPE_SMS = 'sms';
58
    const SCOPE_STATUS_UPDATE = 'status_update';
59
    // Extended Profile Properties
60
    const SCOPE_USER_POSTS = 'user_posts';
61
    const SCOPE_USER_FRIENDS = 'user_friends';
62
    const SCOPE_USER_ABOUT = 'user_about_me';
63
    const SCOPE_USER_TAGGED_PLACES = 'user_tagged_places';
64
    const SCOPE_FRIENDS_ABOUT = 'friends_about_me';
65
    const SCOPE_USER_ACTIVITIES = 'user_activities';
66
    const SCOPE_FRIENDS_ACTIVITIES = 'friends_activities';
67
    const SCOPE_USER_BIRTHDAY = 'user_birthday';
68
    const SCOPE_FRIENDS_BIRTHDAY = 'friends_birthday';
69
    const SCOPE_USER_CHECKINS = 'user_checkins';
70
    const SCOPE_FRIENDS_CHECKINS = 'friends_checkins';
71
    const SCOPE_USER_EDUCATION = 'user_education_history';
72
    const SCOPE_FRIENDS_EDUCATION = 'friends_education_history';
73
    const SCOPE_USER_EVENTS = 'user_events';
74
    const SCOPE_FRIENDS_EVENTS = 'friends_events';
75
    const SCOPE_USER_GROUPS = 'user_groups';
76
    const SCOPE_USER_MANAGED_GROUPS = 'user_managed_groups';
77
    const SCOPE_FRIENDS_GROUPS = 'friends_groups';
78
    const SCOPE_USER_HOMETOWN = 'user_hometown';
79
    const SCOPE_FRIENDS_HOMETOWN = 'friends_hometown';
80
    const SCOPE_USER_INTERESTS = 'user_interests';
81
    const SCOPE_FRIEND_INTERESTS = 'friends_interests';
82
    const SCOPE_USER_LIKES = 'user_likes';
83
    const SCOPE_FRIENDS_LIKES = 'friends_likes';
84
    const SCOPE_USER_LOCATION = 'user_location';
85
    const SCOPE_FRIENDS_LOCATION = 'friends_location';
86
    const SCOPE_USER_NOTES = 'user_notes';
87
    const SCOPE_FRIENDS_NOTES = 'friends_notes';
88
    const SCOPE_USER_PHOTOS = 'user_photos';
89
    const SCOPE_USER_PHOTO_VIDEO_TAGS = 'user_photo_video_tags';
90
    const SCOPE_FRIENDS_PHOTOS = 'friends_photos';
91
    const SCOPE_FRIENDS_PHOTO_VIDEO_TAGS = 'friends_photo_video_tags';
92
    const SCOPE_USER_QUESTIONS = 'user_questions';
93
    const SCOPE_FRIENDS_QUESTIONS = 'friends_questions';
94
    const SCOPE_USER_RELATIONSHIPS = 'user_relationships';
95
    const SCOPE_FRIENDS_RELATIONSHIPS = 'friends_relationships';
96
    const SCOPE_USER_RELATIONSHIPS_DETAILS = 'user_relationship_details';
97
    const SCOPE_FRIENDS_RELATIONSHIPS_DETAILS = 'friends_relationship_details';
98
    const SCOPE_USER_RELIGION = 'user_religion_politics';
99
    const SCOPE_FRIENDS_RELIGION = 'friends_religion_politics';
100
    const SCOPE_USER_STATUS = 'user_status';
101
    const SCOPE_FRIENDS_STATUS = 'friends_status';
102
    const SCOPE_USER_SUBSCRIPTIONS = 'user_subscriptions';
103
    const SCOPE_FRIENDS_SUBSCRIPTIONS = 'friends_subscriptions';
104
    const SCOPE_USER_VIDEOS = 'user_videos';
105
    const SCOPE_FRIENDS_VIDEOS = 'friends_videos';
106
    const SCOPE_USER_WEBSITE = 'user_website';
107
    const SCOPE_FRIENDS_WEBSITE = 'friends_website';
108
    const SCOPE_USER_WORK = 'user_work_history';
109
    const SCOPE_FRIENDS_WORK = 'friends_work_history';
110
    // Open Graph Permissions
111
    const SCOPE_USER_MUSIC = 'user_actions.music';
112
    const SCOPE_FRIENDS_MUSIC = 'friends_actions.music';
113
    const SCOPE_USER_NEWS = 'user_actions.news';
114
    const SCOPE_FRIENDS_NEWS = 'friends_actions.news';
115
    const SCOPE_USER_VIDEO = 'user_actions.video';
116
    const SCOPE_FRIENDS_VIDEO = 'friends_actions.video';
117
    const SCOPE_USER_APP = 'user_actions:APP_NAMESPACE';
118
    const SCOPE_FRIENDS_APP = 'friends_actions:APP_NAMESPACE';
119
    const SCOPE_USER_GAMES = 'user_games_activity';
120
    const SCOPE_FRIENDS_GAMES = 'friends_games_activity';
121
    //Page Permissions
122
    const SCOPE_PAGES = 'manage_pages';
123
    const SCOPE_PAGES_MESSAGING = 'pages_messaging';
124
    const SCOPE_PUBLISH_PAGES = 'publish_pages';
125
126
    public function __construct(
127
        CredentialsInterface $credentials,
128
        ClientInterface $httpClient,
129
        TokenStorageInterface $storage,
130
        $scopes = [],
131
        ?UriInterface $baseApiUri = null,
132
        $apiVersion = ''
133
    ) {
134
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true, $apiVersion);
135
136
        if (null === $baseApiUri) {
137
            $this->baseApiUri = new Uri('https://graph.facebook.com' . $this->getApiVersionString() . '/');
138
        }
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getAuthorizationEndpoint()
145
    {
146
        return new Uri('https://www.facebook.com' . $this->getApiVersionString() . '/dialog/oauth');
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getAccessTokenEndpoint()
153
    {
154
        return new Uri('https://graph.facebook.com' . $this->getApiVersionString() . '/oauth/access_token');
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    protected function parseAccessTokenResponse($responseBody)
161
    {
162
        $data = @json_decode($responseBody, true);
163
164
        // Facebook gives us a query string on old api (v2.0)
165
        if (!$data) {
166
            parse_str($responseBody, $data);
167
        }
168
169
        if (null === $data || !is_array($data)) {
170
            throw new TokenResponseException('Unable to parse response.');
171
        } elseif (isset($data['error'])) {
172
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
173
        }
174
175
        $token = new StdOAuth2Token();
176
        $token->setAccessToken($data['access_token']);
177
178
        if (isset($data['expires'])) {
179
            $token->setLifeTime($data['expires']);
180
        }
181
182
        if (isset($data['refresh_token'])) {
183
            $token->setRefreshToken($data['refresh_token']);
184
            unset($data['refresh_token']);
185
        }
186
187
        unset($data['access_token'], $data['expires']);
188
189
        $token->setExtraParams($data);
190
191
        return $token;
192
    }
193
194
    public function getDialogUri($dialogPath, array $parameters)
195
    {
196
        if (!isset($parameters['redirect_uri'])) {
197
            throw new Exception('Redirect uri is mandatory for this request');
198
        }
199
        $parameters['app_id'] = $this->credentials->getConsumerId();
200
        $baseUrl = self::WWW_URL . $this->getApiVersionString() . '/dialog/' . $dialogPath;
201
        $query = http_build_query($parameters);
202
203
        return new Uri($baseUrl . '?' . $query);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    protected function getApiVersionString()
210
    {
211
        return empty($this->apiVersion) ? '' : '/v' . $this->apiVersion;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    protected function getScopesDelimiter()
218
    {
219
        return ',';
220
    }
221
}
222