Completed
Pull Request — master (#20)
by Harry
13:19
created

GigyaGrant   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 5
dl 9
loc 45
ccs 23
cts 23
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getToken() 9 23 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 AccessToken|null */
24
    private $token;
25
    /** @var Gigya */
26
    private $gigya;
27
28
    /**
29
     * GigyaGrant constructor.
30
     *
31
     * @param Gigya $gigya
32
     */
33 6
    public function __construct(Gigya $gigya)
34
    {
35 6
        $this->gigya = $gigya;
36 6
        $this->token = null;
37 6
    }
38
39
    /**
40
     * @return AccessToken|null
41
     */
42 4
    public function getToken()
43
    {
44 4
        if (!is_null($this->token) && $this->token->isExpired()) {
45 1
            $this->token = null;
46 1
        }
47
48 4
        if (is_null($this->token)) {
49 4
            $response = $this->gigya->socialize()->getToken([
50 4
                'grant_type' => 'none',
51 4
            ], ['auth' => 'credentials']);
52 4 View Code Duplication
            if ($response->getErrorCode() == ErrorCode::OK) {
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...
53 3
                $data = $response->getData();
54 3
                $token = $data->get('access_token');
55 3
                $expires = null;
56 3
                if ($data->has('expires_in')) {
57 2
                    $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $data->get('expires_in', 0))));
58 2
                }
59 3
                $this->token = new AccessToken($token, $expires);
60 3
            }
61 4
        }
62
63 4
        return $this->token;
64
    }
65
}
66