Completed
Push — master ( d2cebe...dfbba4 )
by Дмитрий
12:05
created

Facebook::getIdentity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0254

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
ccs 17
cts 33
cp 0.5152
rs 9.0303
cc 3
eloc 29
nc 3
nop 1
crap 4.0254
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 3
    public function getBaseUri()
25
    {
26 3
        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 2
    public function getIdentity(AccessTokenInterface $accessToken)
48
    {
49 2
        $response = $this->httpClient->request(
50 2
            $this->getBaseUri() . 'me',
51
            [
52 2
                'access_token' => $accessToken->getToken(),
53 2
                'fields' => $this->getFieldsInline()
54 2
            ]
55 2
        );
56
57 2
        if (!$response->isSuccess()) {
58 1
            throw new InvalidResponse(
59 1
                'API response with error code',
60
                $response
61 1
            );
62
        }
63
64 1
        $result = $response->json();
65 1
        if (!$result) {
66 1
            throw new InvalidResponse(
67 1
                'API response is not a valid JSON object',
68 1
                $response->getBody()
69 1
            );
70
        }
71
72
        $hydrator = new ObjectMap(
73
            [
74
                'id' => 'id',
75
                'first_name' => 'firstname',
76
                'last_name' => 'lastname',
77
                'email' => 'email',
78
                'gender' => 'sex',
79
                'link' => 'url',
80
                'locale' => 'locale',
81
                'name' => 'fullname',
82
                'timezone' => 'timezone',
83
                'updated_time' => 'dateModified',
84
                'verified' => 'verified'
85
            ]
86
        );
87
88
        /** @var User $user */
89
        $user = $hydrator->hydrate(new User(), $result);
90
        $user->emailVerified = true;
91
92
        return $user;
93
    }
94
}
95