Completed
Push — master ( deb54b...06ec1a )
by Дмитрий
01:56
created

src/OAuth1/Provider/Twitter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function getBaseUri()
18
    {
19
        return 'https://api.twitter.com/1.1/';
20
    }
21
22
    public function getAuthorizeUri()
23
    {
24
        return 'https://api.twitter.com/oauth/authenticate';
25
    }
26
27
    public function getRequestTokenUri()
28
    {
29
        return 'https://api.twitter.com/oauth/request_token';
30
    }
31
32
    public function getRequestTokenAccessUri()
33
    {
34
        return 'https://api.twitter.com/oauth/access_token';
35
    }
36
37
    public function getName()
38
    {
39
        return 'twitter';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getIdentity(AccessTokenInterface $accessToken)
46
    {
47
        $this->consumerToken = $accessToken;
0 ignored issues
show
Documentation Bug introduced by
It seems like $accessToken of type object<SocialConnect\Pro...r\AccessTokenInterface> is incompatible with the declared type object<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...
48
49
        $parameters = [
50
            'oauth_consumer_key' => $this->consumer->getKey(),
51
            'oauth_token' => $accessToken->getToken(),
52
            // String is expected because Twitter is awful
53
            'include_email' => 'true'
54
        ];
55
56
        // @link https://dev.twitter.com/rest/reference/get/account/verify_credentials
57
        $response = $this->oauthRequest(
58
            $this->getBaseUri() . 'account/verify_credentials.json',
59
            Client::GET,
60
            $parameters
61
        );
62
63
        if (!$response->isSuccess()) {
64
            throw new InvalidResponse(
65
                'API response with error code',
66
                $response
67
            );
68
        }
69
70
        $result = $response->json();
71
        if (!$result) {
72
            throw new InvalidResponse(
73
                'API response is not a valid JSON object',
74
                $response->getBody()
75
            );
76
        }
77
78
        $hydrator = new ObjectMap(
79
            [
80
                'id' => 'id',
81
                'name' => 'fullname',
82
                'screen_name' => 'username'
83
            ]
84
        );
85
86
        /** @var User $user */
87
        $user = $hydrator->hydrate(new User(), $result);
88
89
        // When set to true email will be returned in the user objects as a string.
90
        // If the user does not have an email address on their account,
91
        // or if the email address is not verified, null will be returned.
92
        $user->emailVerified = true;
93
94
        return $user;
95
    }
96
}
97