Completed
Pull Request — master (#20)
by Harry
08:49
created

GigyaCodeGrant::getToken()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 17
Ratio 77.27 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 22
rs 8.9197
cc 4
eloc 16
nc 4
nop 0
1
<?php
2
3
namespace Graze\Gigya\Auth\OAuth2;
4
5
use DateInterval;
6
use DateTime;
7
use Graze\Gigya\Gigya;
8
use Graze\Gigya\Response\ErrorCode;
9
10
class GigyaCodeGrant implements GrantInterface
11
{
12
    /** @var Gigya */
13
    private $gigya;
14
    /** @var string */
15
    private $code;
16
    /** @var string */
17
    private $redirectUri;
18
    /** @var AccessToken|null */
19
    private $token;
20
21
    /**
22
     * @param Gigya  $gigya
23
     * @param string $code
24
     * @param string $redirectUri
25
     */
26
    public function __construct(Gigya $gigya, $code, $redirectUri)
27
    {
28
        $this->gigya = $gigya;
29
        $this->code = $code;
30
        $this->redirectUri = $redirectUri;
31
    }
32
33
    /**
34
     * @return AccessToken|null
35
     */
36
    public function getToken()
37
    {
38 View Code Duplication
        if (is_null($this->token)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            $response = $this->gigya->socialize()->getToken([
40
                'authorization_code' => $this->code,
41
                'redirect_uri'       => $this->redirectUri,
42
                'grant_type'         => 'code',
43
            ], ['auth' => 'none']);
44
            if ($response->getErrorCode() == ErrorCode::OK) {
45
                $data = $response->getData();
46
                $token = $data->get('access_token');
47
                $expiresIn = $data->get('expires_in', null);
48
                $expires = null;
49
                if (!is_null($expiresIn)) {
50
                    $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $expiresIn)));
51
                }
52
                $this->token = new AccessToken($token, $expires);
53
            }
54
        }
55
56
        return $this->token;
57
    }
58
}
59