Completed
Push — master ( 0ac77b...71996c )
by Rafael
19s queued 16s
created

Auth   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 12.71 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 15
loc 118
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A login() 0 12 3
A setClientCredentials() 0 6 1
A setClientId() 0 5 1
A setClientSecret() 0 5 1
A setGrantType() 0 5 1
A getToken() 0 8 3
A auth() 15 15 1

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 WeDevBr\Bankly\Auth;
4
5
use Illuminate\Support\Facades\Http;
6
7
/**
8
 * Class Auth
9
 *
10
 * @author Rafael Teixeira <[email protected]>
11
 * @package WeDevBr\Bankly
12
 */
13
final class Auth
14
{
15
    /** @var self */
16
    private static $login;
17
18
    /** @var string */
19
    protected $loginUrl;
20
21
    /** @var string */
22
    private $clientId;
23
24
    /** @var string */
25
    private $clientSecret;
26
27
    /** @var string */
28
    protected $grantType = 'client_credentials';
29
30
    /** @var string */
31
    private $token;
32
33
    /** @var string */
34
    private $tokenExpiry;
35
36
    private function __construct()
37
    {
38
        //
39
    }
40
41
    /**
42
     * Returns the instance of this class
43
     *
44
     * @param string|null $loginUrl
45
     * @return self
46
     */
47
    public static function login($loginUrl = null)
48
    {
49
        if (is_null(self::$login)) {
50
            self::$login = new Auth();
51
        }
52
53
        if (is_null($loginUrl)) {
54
            self::$login->loginUrl = config('bankly')['login_url'];
55
        }
56
57
        return self::$login;
58
    }
59
60
    /**
61
     * @return self
62
     */
63
    public function setClientCredentials()
64
    {
65
        $this->clientId = $this->clientId ?? config('bankly')['client_id'];
66
        $this->clientSecret = $this->clientSecret ?? config('bankly')['client_secret'];
67
        return $this;
68
    }
69
70
    /**
71
     * @param null|string $clientId
72
     * @return self
73
     */
74
    public function setClientId($clientId)
75
    {
76
        $this->clientId = $clientId;
77
        return $this;
78
    }
79
80
    /**
81
     * @param null|string $clientSecret
82
     * @return self
83
     */
84
    public function setClientSecret($clientSecret)
85
    {
86
        $this->clientSecret = $clientSecret;
87
        return $this;
88
    }
89
90
    /**
91
     * @param string $grantType
92
     * @return self
93
     */
94
    public function setGrantType(string $grantType)
95
    {
96
        $this->grantType = $grantType;
97
        return $this;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getToken()
104
    {
105
        if (now()->unix() > $this->tokenExpiry || !$this->token) {
106
            $this->auth();
107
        }
108
109
        return $this->token;
110
    }
111
112
    /**
113
     * @return void
114
     */
115 View Code Duplication
    private function auth(): void
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...
116
    {
117
        $this->setClientCredentials();
118
119
        //TODO: Add auth for username and password
120
        $body = [
121
            'grant_type' => $this->grantType,
122
            'client_secret' => $this->clientSecret,
123
            'client_id' => $this->clientId
124
        ];
125
126
        $response = Http::asForm()->post($this->loginUrl, $body)->throw()->json();
127
        $this->token = $response['access_token'];
128
        $this->tokenExpiry = now()->addSeconds($response['expires_in'])->unix();
129
    }
130
}
131