BaseProvider::getAccessToken()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 2
nop 3
dl 0
loc 21
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Oauth\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Token\AccessToken;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
9
abstract class BaseProvider extends AbstractProvider
10
{
11
    public $generator;
12
13
    public function __construct(array $options = [], array $collaborators = [], UrlGeneratorInterface $generator = null)
14
    {
15
        $this->generator = $generator;
16
17
        parent::__construct($options, $collaborators);
18
    }
19
20
    public function getRedirectUri()
21
    {
22
        return $this->redirectUri;
23
    }
24
25
    /**
26
     * Requests an access token using a specified grant and option set.
27
     *
28
     * @param  mixed $grant
29
     * @param  array $options
30
     *
31
     * @param array  $attributes
32
     *
33
     * @return AccessToken
34
     * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
35
     * @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
36
     * @throws \Symfony\Component\Routing\Exception\InvalidParameterException
37
     */
38
    public function getAccessToken($grant, array $options = [], array $attributes = [])
39
    {
40
        $grant = $this->verifyGrant($grant);
41
42
        $redirectUri = null;
43
        if (!empty($attributes) && $this->generator) {
44
            $redirectUri = $this->generator->generate($attributes['_route'], $attributes['_route_params'], UrlGeneratorInterface::ABSOLUTE_URL);
45
        }
46
47
        $params = [
48
            'client_id' => $this->clientId,
49
            'client_secret' => $this->clientSecret,
50
            'redirect_uri' => $redirectUri ?: $this->redirectUri,
51
        ];
52
53
        $params = $grant->prepareRequestParameters($params, $options);
54
        $request = $this->getAccessTokenRequest($params);
55
        $response = $this->getParsedResponse($request);
56
        $prepared = $this->prepareAccessTokenResponse($response);
57
58
        return $this->createAccessToken($prepared, $grant);
59
    }
60
61
    public function setState($state = null)
62
    {
63
        $this->state = $state;
64
    }
65
}
66