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

GigyaGrant   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 28.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 5
dl 17
loc 60
ccs 25
cts 25
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C getToken() 17 26 7

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 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