Completed
Pull Request — master (#100)
by
unknown
02:13
created

LinkedinProvider::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Socialite\Providers;
13
14
use Overtrue\Socialite\AccessTokenInterface;
15
use Overtrue\Socialite\ProviderInterface;
16
use Overtrue\Socialite\User;
17
18
/**
19
 * Class LinkedinProvider.
20
 *
21
 * @see https://developer.linkedin.com/docs/oauth2 [Authenticating with OAuth 2.0]
22
 */
23
class LinkedinProvider extends AbstractProvider implements ProviderInterface
24
{
25
    /**
26
     * The scopes being requested.
27
     *
28
     * @var array
29
     */
30
    protected $scopes = ['r_basicprofile', 'r_emailaddress'];
31
32
    /**
33
     * The fields that are included in the profile.
34
     *
35
     * @var array
36
     */
37
    protected $fields = [
38
        'id', 'first-name', 'last-name', 'formatted-name',
39
        'email-address', 'headline', 'location', 'industry',
40
        'public-profile-url', 'picture-url', 'picture-urls::(original)',
41
    ];
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function getAuthUrl($state)
47
    {
48
        return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state);
49
    }
50
51
    /**
52
     * Get the access token for the given code.
53
     *
54
     * @param string $code
55
     *
56
     * @return \Overtrue\Socialite\AccessToken
57
     */
58 View Code Duplication
    public function getAccessToken($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $response = $this->getHttpClient()
61
                         ->post($this->getTokenUrl(), ['form_params' => $this->getTokenFields($code)]);
62
63
        return $this->parseAccessToken($response->getBody());
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getTokenUrl()
70
    {
71
        return 'https://www.linkedin.com/oauth/v2/accessToken';
72
    }
73
74
    /**
75
     * Get the POST fields for the token request.
76
     *
77
     * @param string $code
78
     *
79
     * @return array
80
     */
81
    protected function getTokenFields($code)
82
    {
83
        return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function getUserByToken(AccessTokenInterface $token)
90
    {
91
        $fields = implode(',', $this->fields);
92
93
        $url = 'https://api.linkedin.com/v1/people/~:('.$fields.')';
94
95
        $response = $this->getHttpClient()->get($url, [
96
            'headers' => [
97
                'x-li-format' => 'json',
98
                'Authorization' => 'Bearer '.$token,
99
            ],
100
        ]);
101
102
        return json_decode($response->getBody(), true);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    protected function mapUserToObject(array $user)
109
    {
110
        return new User([
111
            'id' => $this->arrayItem($user, 'id'),
112
            'nickname' => $this->arrayItem($user, 'formattedName'),
113
            'name' => $this->arrayItem($user, 'formattedName'),
114
            'email' => $this->arrayItem($user, 'emailAddress'),
115
            'avatar' => $this->arrayItem($user, 'pictureUrl'),
116
            'avatar_original' => $this->arrayItem($user, 'pictureUrls.values.0'),
117
        ]);
118
    }
119
120
    /**
121
     * Set the user fields to request from LinkedIn.
122
     *
123
     * @param array $fields
124
     *
125
     * @return $this
126
     */
127
    public function fields(array $fields)
128
    {
129
        $this->fields = $fields;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Determine if the provider is operating as stateless.
136
     *
137
     * @return bool
138
     */
139
    protected function isStateless()
140
    {
141
        return true;
142
    }
143
}
144