Completed
Push — master ( d92cdc...6096f9 )
by Brian
17:02 queued 07:04
created

AbstractProvider::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
use Laravel\Socialite\Two\AbstractProvider as BaseProvider;
11
12
abstract class AbstractProvider extends BaseProvider implements ProviderInterface
13
{
14
    use ConfigTrait;
15
16
    /**
17
     * @var array
18 1
     */
19
    protected $credentialsResponseBody;
20 1
21
    public static function serviceContainerKey($providerName)
22
    {
23 7
        return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName;
24
    }
25 7
26
    /**
27
     * @return \SocialiteProviders\Manager\OAuth2\User
28
     */
29
    public function user()
30
    {
31
        if ($this->hasInvalidState()) {
32
            throw new InvalidStateException();
33 1
        }
34
35 1
        $user = $this->mapUserToObject($this->getUserByToken(
36 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...
37 1
        ));
38 1
39
        $user->setToken($token);
40 1
41
        if ($user instanceof User) {
42
            return $user->setAccessTokenResponseBody($this->credentialsResponseBody);
43
        }
44
45
        return $user;
46 5
    }
47
48 5
    /**
49 2
     * Get the access token for the given code.
50
     *
51
     * @param string $code
52 3
     *
53 3
     * @return string
54 3
     */
55
    public function getAccessToken($code)
56 3
    {
57
        $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
58 3
59 3
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
60
            'headers' => ['Accept' => 'application/json'],
61
            $postKey => $this->getTokenFields($code),
62
        ]);
63
64
        $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...
65
66
        return $this->parseAccessToken($response->getBody());
67
    }
68
69
    /**
70
     * Get the access token from the token response body.
71
     *
72 3
     * @param  string  $body
73
     * @return string
74 3
     */
75
    protected function parseAccessToken($body)
76 3
    {
77 3
        return json_decode($body, true)['access_token'];
78 3
    }
79 3
    
80
    /**
81 3
     * Get the access token response for the given code.
82
     *
83 3
     * @todo Remove this method once all Providers have
84
     *       been updated to use the new getAccessTokenResponse.
85
     * 
86
     * @param  string  $code
87
     * @return array
88
     */
89
    public function getAccessToken($code)
90
    {
91
        return $this->getAccessTokenResponse($code);
0 ignored issues
show
Bug introduced by
The method getAccessTokenResponse() does not exist on SocialiteProviders\Manager\OAuth2\AbstractProvider. Did you maybe mean getAccessToken()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
92
    }
93
}
94