Completed
Push — master ( 64eea6...c69513 )
by Steven
9s
created

LinkedIn::withResourceOwnerVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace League\OAuth2\Client\Provider;
4
5
use InvalidArgumentException;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class LinkedIn extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait;
14
15
    /**
16
     * Default scopes
17
     *
18
     * @var array
19
     */
20
    public $defaultScopes = [];
21
22
    /**
23
     * Preferred resource owner version.
24
     *
25
     * Options: 1,2
26
     *
27
     * @var integer
28
     */
29
    public $resourceOwnerVersion = 1;
30
31
    /**
32
     * Requested fields in scope, seeded with default values
33
     *
34
     * @var array
35
     * @see https://developer.linkedin.com/docs/fields/basic-profile
36
     */
37
    protected $fields = [
38
        'id', 'email-address', 'first-name', 'last-name', 'headline',
39
        'location', 'industry', 'picture-url', 'public-profile-url',
40
        'summary',
41
    ];
42
43
    /**
44
     * Constructs an OAuth 2.0 service provider.
45 30
     *
46
     * @param array $options An array of options to set on this provider.
47 30
     *     Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
48 3
     *     Individual providers may introduce more options, as needed.
49
     * @param array $collaborators An array of collaborators that may be used to
50
     *     override this provider's default behavior. Collaborators include
51 30
     *     `grantFactory`, `requestFactory`, and `httpClient`.
52 30
     *     Individual providers may introduce more collaborators, as needed.
53
     */
54
    public function __construct(array $options = [], array $collaborators = [])
55
    {
56
        if (isset($options['fields']) && !is_array($options['fields'])) {
57
            throw new InvalidArgumentException('The fields option must be an array');
58
        }
59 9
60
        parent::__construct($options, $collaborators);
61 9
    }
62
63
    /**
64
     * Get the string used to separate scopes.
65
     *
66
     * @return string
67
     */
68
    protected function getScopeSeparator()
69 9
    {
70
        return ' ';
71 9
    }
72
73
    /**
74
     * Get authorization url to begin OAuth flow
75
     *
76
     * @return string
77
     */
78
    public function getBaseAuthorizationUrl()
79 15
    {
80
        return 'https://www.linkedin.com/oauth/v2/authorization';
81 15
    }
82
83
    /**
84
     * Get access token url to retrieve token
85
     *
86
     * @return string
87
     */
88
    public function getBaseAccessTokenUrl(array $params)
89
    {
90
        return 'https://www.linkedin.com/oauth/v2/accessToken';
91 6
    }
92
93 6
    /**
94
     * Get provider url to fetch user details
95 6
     *
96
     * @param  AccessToken $token
97
     *
98
     * @return string
99
     */
100
    public function getResourceOwnerDetailsUrl(AccessToken $token)
101
    {
102
        $fields = implode(',', $this->fields);
103
104
        switch ($this->resourceOwnerVersion) {
105
            case 1:
106 6
                return 'https://api.linkedin.com/v1/people/~?format=json&fields='.$fields;
107
            case 2:
108 6
            default:
109
                return 'https://api.linkedin.com/v2/me?fields='.$fields;
110
        }
111
    }
112
113
    /**
114
     * Get the default scopes used by this provider.
115
     *
116
     * This should not be a complete list of all scopes, but the minimum
117
     * required for the provider user interface!
118
     *
119 12
     * @return array
120
     */
121 12
    protected function getDefaultScopes()
122 3
    {
123 4
        return $this->defaultScopes;
124 3
    }
125 2
126 1
    /**
127
     * Check a provider response for errors.
128 9
     *
129
     * @throws IdentityProviderException
130
     * @param  ResponseInterface $response
131
     * @param  string $data Parsed response data
132
     * @return void
133
     */
134
    protected function checkResponse(ResponseInterface $response, $data)
135
    {
136
        if (isset($data['error'])) {
137 6
            throw new IdentityProviderException(
138
                $data['error_description'] ?: $response->getReasonPhrase(),
139 6
                $response->getStatusCode(),
140
                $response
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a array|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
            );
142
        }
143
    }
144
145
    /**
146
     * Generate a user object from a successful user details request.
147 3
     *
148
     * @param array $response
149 3
     * @param AccessToken $token
150
     * @return LinkedInResourceOwner
151
     */
152
    protected function createResourceOwner(array $response, AccessToken $token)
153
    {
154
        return new LinkedInResourceOwner($response);
155
    }
156
157
    /**
158
     * Returns the requested fields in scope.
159 3
     *
160
     * @return array
161 3
     */
162
    public function getFields()
163 3
    {
164
        return $this->fields;
165
    }
166
167
    /**
168
     * Returns the preferred resource owner version.
169
     *
170
     * @return integer
171
     */
172
    public function getResourceOwnerVersion()
173
    {
174
        return $this->resourceOwnerVersion;
175
    }
176
177
    /**
178
     * Updates the requested fields in scope.
179
     *
180
     * @param  array   $fields
181
     *
182
     * @return LinkedIn
183
     */
184
    public function withFields(array $fields)
185
    {
186
        $this->fields = $fields;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Updates the preferred resource owner version.
193
     *
194
     * @param integer $resourceOwnerVersion
195
     *
196
     * @return LinkedIn
197
     */
198
    public function withResourceOwnerVersion($resourceOwnerVersion)
199
    {
200
        $resourceOwnerVersion = (int) $resourceOwnerVersion;
201
202
        if (in_array($resourceOwnerVersion, [1, 2])) {
203
            $this->resourceOwnerVersion = $resourceOwnerVersion;
204
        }
205
206
        return $this;
207
    }
208
}
209