Completed
Push — master ( 0095fb...15ea44 )
by Дмитрий
04:21
created

Facebook::getIdentity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.8323

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 11
cts 27
cp 0.4074
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 24
nc 2
nop 1
crap 2.8323
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidResponse;
11
use SocialConnect\Common\Entity\User;
12
use SocialConnect\Common\Http\Client\Client;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
15
class Facebook extends \SocialConnect\OAuth2\AbstractProvider
16
{
17
    /**
18
     * By default AbstractProvider use POST method, FB does not accept POST and return HTML page ᕙ(⇀‸↼‶)ᕗ
19
     *
20
     * @var string
21
     */
22
    protected $requestHttpMethod = Client::GET;
23
24 2
    public function getBaseUri()
25
    {
26 2
        return 'https://graph.facebook.com/v2.8/';
27
    }
28
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://www.facebook.com/dialog/oauth';
32
    }
33
34 2
    public function getRequestTokenUri()
35
    {
36 2
        return 'https://graph.facebook.com/oauth/access_token';
37
    }
38
39 3
    public function getName()
40
    {
41 3
        return 'facebook';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function getIdentity(AccessTokenInterface $accessToken)
48
    {
49 1
        $response = $this->httpClient->request(
50 1
            $this->getBaseUri() . 'me',
51
            [
52 1
                'access_token' => $accessToken->getToken(),
53 1
                'fields' => $this->getFieldsInline()
54 1
            ]
55 1
        );
56
57 1
        if (!$response->isSuccess()) {
58 1
            throw new InvalidResponse(
59 1
                'API response with error code',
60
                $response
61 1
            );
62
        }
63
64
        $body = $response->getBody();
65
        $result = json_decode($body);
66
67
        $hydrator = new ObjectMap(
68
            [
69
                'id' => 'id',
70
                'first_name' => 'firstname',
71
                'last_name' => 'lastname',
72
                'email' => 'email',
73
                'gender' => 'sex',
74
                'link' => 'url',
75
                'locale' => 'locale',
76
                'name' => 'fullname',
77
                'timezone' => 'timezone',
78
                'updated_time' => 'dateModified',
79
                'verified' => 'verified'
80
            ]
81
        );
82
83
        return $hydrator->hydrate(new User(), $result);
84
    }
85
}
86