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

GigyaCodeGrant   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 34.69 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 5
dl 17
loc 49
ccs 0
cts 27
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getToken() 17 22 4

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