Completed
Pull Request — master (#58)
by Rafael
02:58
created

Auth::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
class Auth
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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
    /**
37
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
     */
39
    private function __construct()
40
    {
41
        //
42
    }
43
44
    /**
45
     * Returns the instance of this class
46
     *
47
     * @param string|null $loginUrl
48
     * @return self
49
     */
50
    public static function login($loginUrl = null)
51
    {
52
        if (is_null(self::$login)) {
53
            self::$login = new Auth();
54
        }
55
56
        if (is_null($loginUrl)) {
57
            self::$login->loginUrl = config('bankly')['login_url'];
58
        }
59
60
        return self::$login;
61
    }
62
63
    /**
64
     * @return self
65
     */
66
    public function setClientCredentials()
67
    {
68
        $this->clientId = $this->clientId ?? config('bankly')['client_id'];
69
        $this->clientSecret = $this->clientSecret ?? config('bankly')['client_secret'];
70
        return $this;
71
    }
72
73
    /**
74
     * @param null|string $clientId
75
     * @return self
76
     */
77
    public function setClientId($clientId)
78
    {
79
        $this->clientId = $clientId;
80
        return $this;
81
    }
82
83
    /**
84
     * @param null|string $clientSecret
85
     * @return self
86
     */
87
    public function setClientSecret($clientSecret)
88
    {
89
        $this->clientSecret = $clientSecret;
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $grantType
95
     * @return self
96
     */
97
    public function setGrantType(string $grantType)
98
    {
99
        $this->grantType = $grantType;
100
        return $this;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getToken()
107
    {
108
        if (now()->unix() > $this->tokenExpiry || !$this->token) {
109
            $this->auth();
110
        }
111
112
        return $this->token;
113
    }
114
115
    /**
116
     * @return void
117
     */
118 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...
119
    {
120
        $this->setClientCredentials();
121
122
        //TODO: Add auth for username and password
123
        $body = [
124
            'grant_type' => $this->grantType,
125
            'client_secret' => $this->clientSecret,
126
            'client_id' => $this->clientId
127
        ];
128
129
        $response = Http::asForm()->post($this->loginUrl, $body)->throw()->json();
130
        $this->token = $response['access_token'];
131
        $this->tokenExpiry = now()->addSeconds($response['expires_in'])->unix();
132
    }
133
}
134