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

GigyaGrant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
crap 1
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Auth\OAuth2;
15
16
use DateInterval;
17
use DateTime;
18
use Graze\Gigya\Gigya;
19
use Graze\Gigya\Response\ErrorCode;
20
21
class GigyaGrant implements GrantInterface
22
{
23
    /** @var string */
24
    private $apiKey;
25
    /** @var string */
26
    private $secret;
27
    /** @var null|string */
28
    private $userKey;
29
    /** @var AccessToken|null */
30
    private $token;
31
    /** @var Gigya */
32
    private $gigya;
33
34
    /**
35
     * GigyaGrant constructor.
36
     *
37
     * @param Gigya       $gigya
38
     * @param string      $apiKey
39
     * @param string      $secret
40
     * @param string|null $userKey
41
     */
42 6
    public function __construct(Gigya $gigya, $apiKey, $secret, $userKey = null)
43
    {
44 6
        $this->gigya = $gigya;
45 6
        $this->apiKey = $apiKey;
46 6
        $this->secret = $secret;
47 6
        $this->userKey = $userKey;
48 6
        $this->token = null;
49 6
    }
50
51
    /**
52
     * @return AccessToken|null
53
     */
54 4
    public function getToken()
55
    {
56 4
        if (!is_null($this->token) && $this->token->isExpired()) {
57 1
            $this->token = null;
58
        }
59
60 4 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...
61 4
            $response = $this->gigya->socialize()->getToken([
62 4
                'client_id'     => $this->userKey ?: $this->apiKey,
63 4
                'client_secret' => $this->secret,
64 4
                'grant_type'    => 'none',
65 4
            ], ['auth' => 'none']);
66 4
            if ($response->getErrorCode() == ErrorCode::OK) {
67 3
                $data = $response->getData();
68 3
                $token = $data->get('access_token');
69 3
                $expiresIn = $data->get('expires_in', null);
70 3
                $expires = null;
71 3
                if (!is_null($expiresIn)) {
72 2
                    $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $expiresIn)));
73
                }
74 3
                $this->token = new AccessToken($token, $expires);
75
            }
76
        }
77
78 4
        return $this->token;
79
    }
80
}
81