Completed
Push — master ( 9ab7a4...7b2d5d )
by Дмитрий
02:51
created

Facebook::getIdentity()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.5602

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 16
cts 35
cp 0.4571
rs 8.8981
cc 4
eloc 31
nc 4
nop 1
crap 6.5602

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\OAuth2\AccessToken;
10
use SocialConnect\Provider\AccessTokenInterface;
11
use SocialConnect\Provider\Exception\InvalidAccessToken;
12
use SocialConnect\Provider\Exception\InvalidResponse;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Http\Client\Client;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
17
class Facebook extends \SocialConnect\OAuth2\AbstractProvider
18
{
19
    /**
20
     * By default AbstractProvider use POST method, FB does not accept POST and return HTML page ᕙ(⇀‸↼‶)ᕗ
21
     *
22
     * @var string
23
     */
24
    protected $requestHttpMethod = Client::GET;
25
26 3
    public function getBaseUri()
27
    {
28 3
        return 'https://graph.facebook.com/v2.8/';
29
    }
30
31 2
    public function getAuthorizeUri()
32
    {
33 2
        return 'https://www.facebook.com/dialog/oauth';
34
    }
35
36 2
    public function getRequestTokenUri()
37
    {
38 2
        return 'https://graph.facebook.com/oauth/access_token';
39
    }
40
41 3
    public function getName()
42
    {
43 3
        return 'facebook';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function parseToken($body)
50
    {
51 3
        if (empty($body)) {
52
            throw new InvalidAccessToken('Provider response with empty body');
53
        }
54
55 3
        $result = json_decode($body, true);
56 3
        if ($result) {
57 1
            return new AccessToken($result);
58
        }
59
60 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function getIdentity(AccessTokenInterface $accessToken)
67
    {
68 2
        $response = $this->httpClient->request(
69 2
            $this->getBaseUri() . 'me',
70
            [
71 2
                'access_token' => $accessToken->getToken(),
72 2
                'fields' => $this->getFieldsInline()
73 2
            ]
74 2
        );
75
76 2
        if (!$response->isSuccess()) {
77 1
            throw new InvalidResponse(
78 1
                'API response with error code',
79
                $response
80 1
            );
81
        }
82
83 1
        $result = $response->json();
84 1
        if (!$result) {
85 1
            throw new InvalidResponse(
86 1
                'API response is not a valid JSON object',
87
                $response
88 1
            );
89
        }
90
91
        $hydrator = new ObjectMap(
92
            [
93
                'id' => 'id',
94
                'first_name' => 'firstname',
95
                'last_name' => 'lastname',
96
                'email' => 'email',
97
                'gender' => 'sex',
98
                'link' => 'url',
99
                'locale' => 'locale',
100
                'name' => 'fullname',
101
                'timezone' => 'timezone',
102
                'updated_time' => 'dateModified',
103
                'verified' => 'verified'
104
            ]
105
        );
106
107
        /** @var User $user */
108
        $user = $hydrator->hydrate(new User(), $result);
109
        $user->emailVerified = true;
110
111
        if ($result->picture) {
112
            $user->pictureURL = $result->picture->data->url;
113
        }
114
115
        return $user;
116
    }
117
}
118