Completed
Pull Request — master (#20)
by Harry
08:55
created

GigyaCodeGrant   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 30.16 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 6
c 2
b 0
f 2
lcom 1
cbo 5
dl 19
loc 63
ccs 26
cts 26
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
B getToken() 10 24 5

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
    /** @var string */
21
    private $apiKey;
22
    /** @var string */
23
    private $secret;
24
    /** @var string|null */
25
    private $userKey;
26
27
    /**
28
     * @param Gigya       $gigya
29
     * @param string      $code
30
     * @param string      $redirectUri
31
     * @param string      $apiKey
32
     * @param string      $secret
33
     * @param string|null $userKey
34
     */
35 5 View Code Duplication
    public function __construct(Gigya $gigya, $code, $redirectUri, $apiKey, $secret, $userKey = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
36
    {
37 5
        $this->gigya = $gigya;
38 5
        $this->code = $code;
39 5
        $this->redirectUri = $redirectUri;
40 5
        $this->apiKey = $apiKey;
41 5
        $this->secret = $secret;
42 5
        $this->userKey = $userKey;
43 5
    }
44
45
    /**
46
     * @return AccessToken|null
47
     */
48 4
    public function getToken()
49
    {
50 4
        if (is_null($this->token)) {
51 4
            $response = $this->gigya->socialize()->getToken([
52 4
                'client_id'     => $this->userKey ?: $this->apiKey,
53 4
                'client_secret' => $this->secret,
54 4
                'code'          => $this->code,
55 4
                'redirect_uri'  => $this->redirectUri,
56 4
                'grant_type'    => 'authorization_code',
57 4
            ], ['auth' => 'none']);
58 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...
59 4
                $data = $response->getData();
60 4
                $token = $data->get('access_token');
61 4
                $expiresIn = $data->get('expires_in', null);
62 4
                $expires = null;
63 4
                if (!is_null($expiresIn)) {
64 3
                    $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $expiresIn)));
65
                }
66 4
                $this->token = new AccessToken($token, $expires);
67
            }
68
        }
69
70 4
        return $this->token;
71
    }
72
}
73