Completed
Push — master ( 1a26f0...d92cdc )
by Brian
07:26
created

AbstractProvider::user()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
crap 3
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())
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);
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
    $this->getAccessTokenResponse($this->getCode())
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
81 3
    
82
    /**
83 3
     * Get the access token response for the given code.
84
     *
85
     * @todo Remove this method once all Providers have
86
     *       been updated to use the new getAccessTokenResponse.
87
     * 
88
     * @param  string  $code
89
     * @return array
90
     */
91
    public function getAccessToken($code)
92
    {
93
        return $this->getAccessTokenResponse($code);
94
    }
95
}
96