Completed
Pull Request — master (#23)
by
unknown
13:03
created

LinkedIn::withResourceOwnerVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 = ['r_liteprofile'];
21
22
23
    /**
24
     * Requested fields in scope, seeded with default values
25
     *
26
     * @var array
27
     * @see https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin?context=linkedin/consumer/context#response-body-schema
28
     */
29
    protected $fields = [
30
        'id', 'firstName', 'lastName', 'profilePicture'
31
    ];
32
33
    /**
34
     * Constructs an OAuth 2.0 service provider.
35
     *
36
     * @param array $options An array of options to set on this provider.
37
     *     Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
38
     *     Individual providers may introduce more options, as needed.
39
     * @param array $collaborators An array of collaborators that may be used to
40
     *     override this provider's default behavior. Collaborators include
41
     *     `grantFactory`, `requestFactory`, and `httpClient`.
42
     *     Individual providers may introduce more collaborators, as needed.
43
     */
44
    public function __construct(array $options = [], array $collaborators = [])
45
    {
46
        if (isset($options['fields']) && !is_array($options['fields'])) {
47
            throw new InvalidArgumentException('The fields option must be an array');
48
        }
49
50
        parent::__construct($options, $collaborators);
51
    }
52
53
    /**
54 36
     * Get the string used to separate scopes.
55
     *
56 36
     * @return string
57 3
     */
58
    protected function getScopeSeparator()
59
    {
60 36
        return ' ';
61 36
    }
62
63
    /**
64
     * Get authorization url to begin OAuth flow
65
     *
66
     * @return string
67
     */
68 9
    public function getBaseAuthorizationUrl()
69
    {
70 9
        return 'https://www.linkedin.com/oauth/v2/authorization';
71
    }
72
73
    /**
74
     * Get access token url to retrieve token
75
     *
76
     * @return string
77
     */
78 9
    public function getBaseAccessTokenUrl(array $params)
79
    {
80 9
        return 'https://www.linkedin.com/oauth/v2/accessToken';
81
    }
82
83
    /**
84
     * Get provider url to fetch user details
85
     *
86
     * @param  AccessToken $token
87
     *
88 15
     * @return string
89
     */
90 15
    public function getResourceOwnerDetailsUrl(AccessToken $token)
91
    {
92
        return 'https://api.linkedin.com/v2/me?fields=' . implode(',', $this->fields);
93
    }
94
95
    /**
96
     * Get the default scopes used by this provider.
97
     *
98
     * This should not be a complete list of all scopes, but the minimum
99
     * required for the provider user interface!
100 12
     *
101
     * @return array
102 12
     */
103
    protected function getDefaultScopes()
104 12
    {
105 12
        return $this->defaultScopes;
106 12
    }
107 3
108 1
    /**
109 3
     * Check a provider response for errors.
110
     *
111 1
     * @throws IdentityProviderException
112
     * @param  ResponseInterface $response
113
     * @param  string $data Parsed response data
114
     * @return void
115
     */
116
    protected function checkResponse(ResponseInterface $response, $data)
117
    {
118
        if (isset($data['error'])) {
119
            throw new IdentityProviderException(
120
                $data['error_description'] ?: $response->getReasonPhrase(),
121 6
                $response->getStatusCode(),
122
                $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...
123 6
            );
124
        }
125
    }
126
127
    /**
128
     * Generate a user object from a successful user details request.
129
     *
130
     * @param array $response
131
     * @param AccessToken $token
132
     * @return LinkedInResourceOwner
133
     */
134 12
    protected function createResourceOwner(array $response, AccessToken $token)
135
    {
136 12
        return new LinkedInResourceOwner($response);
137 3
    }
138 3
139 3
    /**
140 2
     * Returns the requested fields in scope.
141 1
     *
142
     * @return array
143 9
     */
144
    public function getFields()
145
    {
146
        return $this->fields;
147
    }
148
149
150
    /**
151
     * Updates the requested fields in scope.
152 6
     *
153
     * @param  array   $fields
154 6
     *
155
     * @return LinkedIn
156
     */
157
    public function withFields(array $fields)
158
    {
159
        $this->fields = $fields;
160
161
        return $this;
162 9
    }
163
}
164