Completed
Push — master ( a294ea...2758a3 )
by Дмитрий
06:11
created

LinkedIn::getBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Bogdan Popa https://github.com/icex <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
use SocialConnect\OAuth2\AccessToken;
15
16
class LinkedIn extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 3
    public function getBaseUri()
22
    {
23 3
        return 'https://api.linkedin.com/v1/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://www.linkedin.com/oauth/v2/authorization';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://www.linkedin.com/oauth/v2/accessToken';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return 'linkedin';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 3
    public function parseToken($body)
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('AccessToken is not a 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() .
70 2
            'people/~:(id,first-name,last-name,email-address,picture-url,location:(name))?format=json',
71 2
            [], 'GET',
72
            [
73 2
                'Authorization' => 'Bearer ' . $accessToken->getToken(),
74
            ]
75 2
        );
76
77 2
        if (!$response->isSuccess()) {
78 1
            throw new InvalidResponse(
79 1
                'API response with error code',
80
                $response
81 1
            );
82
        }
83
84 1
        $result = $response->json();
85 1
        if (!$result) {
86 1
            throw new InvalidResponse(
87 1
                'API response is not a valid JSON object',
88
                $response
89 1
            );
90
        }
91
92
        $hydrator = new ObjectMap(
93
            [
94
                'id'           => 'id',
95
                'pictureUrl'   => 'picture',
96
                'emailAddress' => 'email',
97
                'firstName'    => 'firstname',
98
                'lastName'     => 'lastname',
99
            ]
100
        );
101
102
        return $hydrator->hydrate(new User(), $result);
103
    }
104
}
105