Completed
Push — master ( 1b8bc7...9857f5 )
by Дмитрий
02:26
created

LinkedIn::getIdentity()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.512

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
ccs 16
cts 26
cp 0.6153
rs 8.9411
cc 3
eloc 24
nc 3
nop 1
crap 3.512
1
<?php
2
/**
3
 * SocialConnect project
4
 *
5
 * @author: Bogdan Popa https://github.com/icex <[email protected]>
6
 */
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use SocialConnect\Common\Http\Client\Client;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\Provider\Exception\InvalidResponse;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
use SocialConnect\OAuth2\AccessToken;
17
18
class LinkedIn extends \SocialConnect\OAuth2\AbstractProvider
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 3
    public function getBaseUri()
24
    {
25 3
        return 'https://api.linkedin.com/v1/';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function getAuthorizeUri()
32
    {
33 2
        return 'https://www.linkedin.com/oauth/v2/authorization';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function getRequestTokenUri()
40
    {
41 2
        return 'https://www.linkedin.com/oauth/v2/accessToken';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3
    public function getName()
48
    {
49 3
        return 'linkedin';
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    public function parseToken($body)
56
    {
57 3
        $result = json_decode($body, true);
58 3
        if ($result) {
59 1
            return new AccessToken($result);
60
        }
61
62 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function getIdentity(AccessTokenInterface $accessToken)
69
    {
70 2
        $response = $this->httpClient->request(
71 2
            $this->getBaseUri() . 'people/~:(id,first-name,last-name,email-address,picture-url,location:(name))',
72
            [
73
                'format' => 'json'
74 2
            ],
75 2
            Client::GET,
76
            [
77 2
                'Authorization' => 'Bearer ' . $accessToken->getToken(),
78
            ]
79 2
        );
80
81 2
        if (!$response->isSuccess()) {
82 1
            throw new InvalidResponse(
83 1
                'API response with error code',
84
                $response
85 1
            );
86
        }
87
88 1
        $result = $response->json();
89 1
        if (!$result) {
90 1
            throw new InvalidResponse(
91 1
                'API response is not a valid JSON object',
92
                $response
93 1
            );
94
        }
95
96
        $hydrator = new ObjectMap(
97
            [
98
                'id'           => 'id',
99
                'emailAddress' => 'email',
100
                'firstName'    => 'firstname',
101
                'lastName'     => 'lastname',
102
                'pictureUrl'   => 'pictureURL',
103
            ]
104
        );
105
106
        /** @var User $user */
107
        $user = $hydrator->hydrate(new User(), $result);
108
109
        // @todo Remove in 2.0, didnt remove because it's was a bug and I am not interested to break semver!
110
        $user->picture = $user->pictureURL;
111
112
        return $user;
113
    }
114
}
115