Completed
Pull Request — master (#19)
by
unknown
02:53
created

LinkedIn::withFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
     *
46
     * @param array $options An array of options to set on this provider.
47
     *     Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
48
     *     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
     *     `grantFactory`, `requestFactory`, and `httpClient`.
52
     *     Individual providers may introduce more collaborators, as needed.
53
     */
54 36
    public function __construct(array $options = [], array $collaborators = [])
55
    {
56 36
        if (isset($options['fields']) && !is_array($options['fields'])) {
57 3
            throw new InvalidArgumentException('The fields option must be an array');
58
        }
59
60 36
        parent::__construct($options, $collaborators);
61 36
    }
62
63
    /**
64
     * Get the string used to separate scopes.
65
     *
66
     * @return string
67
     */
68 9
    protected function getScopeSeparator()
69
    {
70 9
        return ' ';
71
    }
72
73
    /**
74
     * Get authorization url to begin OAuth flow
75
     *
76
     * @return string
77
     */
78 9
    public function getBaseAuthorizationUrl()
79
    {
80 9
        return 'https://www.linkedin.com/oauth/v2/authorization';
81
    }
82
83
    /**
84
     * Get access token url to retrieve token
85
     *
86
     * @return string
87
     */
88 15
    public function getBaseAccessTokenUrl(array $params)
89
    {
90 15
        return 'https://www.linkedin.com/oauth/v2/accessToken';
91
    }
92
93
    /**
94
     * Get provider url to fetch user details
95
     *
96
     * @param  AccessToken $token
97
     *
98
     * @return string
99
     */
100 12
    public function getResourceOwnerDetailsUrl(AccessToken $token)
101
    {
102 12
        $fields = implode(',', $this->fields);
103
104 12
        switch ($this->resourceOwnerVersion) {
105 12
            case 1:
106 12
                return 'https://api.linkedin.com/v1/people/~:('.$fields.')?format=json';
107 3
            case 2:
108 1
            default:
109 3
                return 'https://api.linkedin.com/v2/me?fields='.$fields;
110
        }
111 1
    }
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
     * @return array
120
     */
121 6
    protected function getDefaultScopes()
122
    {
123 6
        return $this->defaultScopes;
124
    }
125
126
    /**
127
     * Check a provider response for errors.
128
     *
129
     * @throws IdentityProviderException
130
     * @param  ResponseInterface $response
131
     * @param  string $data Parsed response data
132
     * @return void
133
     */
134 12
    protected function checkResponse(ResponseInterface $response, $data)
135
    {
136 12
        if (isset($data['error'])) {
137 3
            throw new IdentityProviderException(
138 3
                $data['error_description'] ?: $response->getReasonPhrase(),
139 3
                $response->getStatusCode(),
140 2
                $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 1
            );
142
        }
143 9
    }
144
145
    /**
146
     * Generate a user object from a successful user details request.
147
     *
148
     * @param array $response
149
     * @param AccessToken $token
150
     * @return LinkedInResourceOwner
151
     */
152 6
    protected function createResourceOwner(array $response, AccessToken $token)
153
    {
154 6
        return new LinkedInResourceOwner($response);
155
    }
156
157
    /**
158
     * Returns the requested fields in scope.
159
     *
160
     * @return array
161
     */
162 9
    public function getFields()
163
    {
164 9
        return $this->fields;
165
    }
166
167
    /**
168
     * Returns the preferred resource owner version.
169
     *
170
     * @return integer
171
     */
172 3
    public function getResourceOwnerVersion()
173
    {
174 3
        return $this->resourceOwnerVersion;
175
    }
176
177
    /**
178
     * Updates the requested fields in scope.
179
     *
180
     * @param  array   $fields
181
     *
182
     * @return LinkedIn
183
     */
184 3
    public function withFields(array $fields)
185
    {
186 3
        $this->fields = $fields;
187
188 3
        return $this;
189
    }
190
191
    /**
192
     * Updates the preferred resource owner version.
193
     *
194
     * @param integer $resourceOwnerVersion
195
     *
196
     * @return LinkedIn
197
     */
198 3
    public function withResourceOwnerVersion($resourceOwnerVersion)
199
    {
200 3
        $resourceOwnerVersion = (int) $resourceOwnerVersion;
201
202 3
        if (in_array($resourceOwnerVersion, [1, 2])) {
203 3
            $this->resourceOwnerVersion = $resourceOwnerVersion;
204 1
        }
205
206 3
        return $this;
207
    }
208
}
209