Completed
Pull Request — master (#20)
by Harry
04:37
created

GigyaCodeGrant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 2
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