Completed
Push — master ( bda8d5...d74c6d )
by Дмитрий
03:32 queued 01:52
created

Twitter::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth1\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 Twitter extends \SocialConnect\OAuth1\AbstractProvider
16
{
17
    const NAME = 'twitter';
18
19
    public function getBaseUri()
20
    {
21
        return 'https://api.twitter.com/1.1/';
22
    }
23
24
    public function getAuthorizeUri()
25
    {
26
        return 'https://api.twitter.com/oauth/authenticate';
27
    }
28
29
    public function getRequestTokenUri()
30
    {
31
        return 'https://api.twitter.com/oauth/request_token';
32
    }
33
34
    public function getRequestTokenAccessUri()
35
    {
36
        return 'https://api.twitter.com/oauth/access_token';
37
    }
38
39
    public function getName()
40
    {
41
        return self::NAME;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getIdentity(AccessTokenInterface $accessToken)
48
    {
49
        $this->consumerToken = $accessToken;
0 ignored issues
show
Documentation Bug introduced by
It seems like $accessToken of type SocialConnect\Provider\AccessTokenInterface is incompatible with the declared type SocialConnect\OAuth1\Token of property $consumerToken.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
51
        $parameters = [
52
            'oauth_consumer_key' => $this->consumer->getKey(),
53
            'oauth_token' => $accessToken->getToken(),
54
            // String is expected because Twitter is awful
55
            'include_email' => 'true'
56
        ];
57
58
        // @link https://dev.twitter.com/rest/reference/get/account/verify_credentials
59
        $response = $this->oauthRequest(
60
            $this->getBaseUri() . 'account/verify_credentials.json',
61
            Client::GET,
62
            $parameters
63
        );
64
65
        if (!$response->isSuccess()) {
66
            throw new InvalidResponse(
67
                'API response with error code',
68
                $response
69
            );
70
        }
71
72
        $result = $response->json();
73
        if (!$result) {
74
            throw new InvalidResponse(
75
                'API response is not a valid JSON object',
76
                $response
77
            );
78
        }
79
80
        $hydrator = new ObjectMap(
81
            [
82
                'id' => 'id',
83
                'name' => 'fullname',
84
                'screen_name' => 'username',
85
                'profile_image_url_https' => 'pictureURL'
86
            ]
87
        );
88
89
        /** @var User $user */
90
        $user = $hydrator->hydrate(new User(), $result);
91
92
        // When set to true email will be returned in the user objects as a string.
93
        // If the user does not have an email address on their account,
94
        // or if the email address is not verified, null will be returned.
95
        $user->emailVerified = true;
96
97
        return $user;
98
    }
99
}
100