Completed
Pull Request — master (#44)
by Brian
10:36
created

AbstractProvider::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SocialiteProviders\Manager\OAuth2;
4
5
use GuzzleHttp\ClientInterface;
6
use Laravel\Socialite\Two\InvalidStateException;
7
use SocialiteProviders\Manager\Contracts\OAuth2\ProviderInterface;
8
use SocialiteProviders\Manager\SocialiteWasCalled;
9
use SocialiteProviders\Manager\ConfigTrait;
10
11
abstract class AbstractProvider extends \Laravel\Socialite\Two\AbstractProvider implements ProviderInterface
12
{
13
    use ConfigTrait;
14
15
    /**
16
     * @var array
17
     */
18 1
    protected $credentialsResponseBody;
19
20 1
    public static function serviceContainerKey($providerName)
21
    {
22
        return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName;
23 7
    }
24
25 7
    /**
26
     * @return \SocialiteProviders\Manager\OAuth2\User
27
     */
28
    public function user()
29
    {
30
        if ($this->hasInvalidState()) {
31
            throw new InvalidStateException();
32
        }
33 1
34
        $user = $this->mapUserToObject($this->getUserByToken(
35 1
            $token = $this->getAccessToken($this->getCode())
0 ignored issues
show
Bug introduced by
It seems like $this->getCode() targeting Laravel\Socialite\Two\AbstractProvider::getCode() can also be of type array; however, SocialiteProviders\Manag...vider::getAccessToken() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
36 1
        ));
37 1
38 1
        $user->setToken($token);
39
40 1
        if ($user instanceof User) {
41
            return $user->setAccessTokenResponseBody($this->credentialsResponseBody);
42
        }
43
44
        return $user;
45
    }
46 5
47
    /**
48 5
     * Get the access token for the given code.
49 2
     *
50
     * @param string $code
51
     *
52 3
     * @return string
53 3
     */
54 3
    public function getAccessToken($code)
55
    {
56 3
        $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
57
58 3
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
59 3
            'headers' => ['Accept' => 'application/json'],
60
            $postKey  => $this->getTokenFields($code),
61
        ]);
62
63
        $this->credentialsResponseBody = json_decode($response->getBody(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($response->getBody(), true) of type * is incompatible with the declared type array of property $credentialsResponseBody.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
65
        return $this->parseAccessToken($response->getBody());
66
    }
67
}
68