Mati::setClientId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace WeDevBr\Mati;
4
5
use LogicException;
6
use WeDevBr\Mati\Support\Contracts\IdentityInputInterface;
7
use WeDevBr\Mati\Support\Contracts\MatiClientInterface;
8
9
/**
10
 * Mati API wrapper class
11
 *
12
 * @author Gabriel Mineiro <[email protected]>
13
 */
14
class Mati
15
{
16
    /**
17
     * Mati API client
18
     *
19
     * @var MatiClientInterface
20
     */
21
    protected $client;
22
    protected $client_id = null;
23
    protected $client_secret = null;
24
25
    /**
26
     * Mati class constructor
27
     *
28
     * @param MatiClientInterface $client
29
     * @param string|null $client_id
30
     * @param string|null $client_secret
31
     */
32
    public function __construct(
33
        MatiClientInterface $client,
34
        string $client_id = null,
35
        string $client_secret = null
36
    ) {
37
        $this->client = $client;
38
        $this->resolveClientId($client_id);
39
        $this->resolveClientSecret($client_secret);
40
41
        if ($this->client_id && $this->client_secret) {
42
            $this->authorize();
43
        }
44
    }
45
46
    /**
47
     * Set Client ID for authorization
48
     *
49
     * @param string $client_id
50
     * @return self
51
     */
52
    public function setClientId(string $client_id)
53
    {
54
        $this->client_id = $client_id;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Set Client Secret for authorization
61
     *
62
     * @param string $client_secret
63
     * @return self
64
     */
65
    public function setClientSecret(string $client_secret)
66
    {
67
        $this->client_secret = $client_secret;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Set API access token
74
     *
75
     * Good to use with caching for JWT token
76
     *
77
     * @param string $access_token
78
     * @return self
79
     */
80
    public function setAccessToken(string $access_token)
81
    {
82
        $this->client->withToken($access_token);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Authorize with Mati's API credentials
89
     *
90
     * @param string|null $client_id
91
     * @param string|null $client_secret
92
     * @return self
93
     */
94
    public function authorize(string $client_id = null, string $client_secret = null)
95
    {
96
        if ($client_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_id of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
97
            $this->setClientId($client_id);
98
        }
99
100
        if ($client_secret) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_secret of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
101
            $this->setClientSecret($client_secret);
102
        }
103
104
        if (!($this->client_id && $this->client_secret)) {
105
            throw new LogicException('No client ID and secret were given to authorize Mati');
106
        }
107
108
        $response = $this->client->getAccessToken($this->client_id, $this->client_secret);
109
110
        $this->setAccessToken($response->object()->access_token);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Alias for authorize()
117
     *
118
     * @see authorize()
119
     */
120
    public function authorise(...$args)
121
    {
122
        return $this->authorize(...$args);
0 ignored issues
show
Documentation introduced by
$args is of type array<integer,?>, but the function expects a null|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
    }
124
125
    /**
126
     * Create a new verification process
127
     *
128
     * @return object
129
     */
130
    public function createVerification(
131
        $metadata = null,
132
        $flow_id = null,
133
        $user_ip = null,
134
        $user_agent = null
135
    ) {
136
        return $this->client->createVerification($metadata, $flow_id, $user_ip, $user_agent)->object();
137
    }
138
139
    /**
140
     * Send input for verification
141
     *
142
     * @param string $identity_id
143
     * @param IdentityInputInterface[]|Collection $inputs
144
     * @return object
145
     */
146
    public function sendInput(string $identity_id, $inputs)
147
    {
148
        return $this->client->sendInput($identity_id, $inputs)->object();
149
    }
150
151
    /**
152
     * Retrieve info about a verification process
153
     *
154
     * @param string $resource_url URL received by webhook
155
     * @return object
156
     */
157
    public function retrieveResourceDataFromUrl(string $resource_url)
158
    {
159
        return $this->client->retrieveResourceDataFromUrl($resource_url)->object();
160
    }
161
162
    /**
163
     * Retrieve info about a verification process
164
     *
165
     * @param string $verification_id
166
     * @return object
167
     */
168
    public function retrieveResourceDataByVerificationId(string $verification_id)
169
    {
170
        return $this->client->retrieveResourceDataFromUrl($verification_id)->object();
171
    }
172
173
    /**
174
     * Download the file sent by the user during the verification process
175
     *
176
     * @param string $media_url
177
     *
178
     * @throws RequestException
179
     * @return string Media contents
180
     */
181
    public function downloadVerificationMedia(string $media_url)
182
    {
183
        return $this->client->downloadVerificationMedia($media_url)->body();
184
    }
185
186
    /**
187
     * Resolve value for Client ID in the constructor
188
     *
189
     * @param string|null $client_id
190
     * @return void
191
     */
192
    protected function resolveClientId($client_id)
193
    {
194
        if ($client_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_id of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
195
            $this->setClientId($client_id);
196
        } else {
197
            $config_client_id = config('mati')['client_id'];
198
            if ($config_client_id) {
199
                $this->setClientId($config_client_id);
200
            }
201
        }
202
    }
203
204
    /**
205
     * Resolve value for Client Secret in the constructor
206
     *
207
     * @param string|null $client_secret
208
     * @return void
209
     */
210
    protected function resolveClientSecret($client_secret)
211
    {
212
        if ($client_secret) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_secret of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
213
            $this->setClientSecret($client_secret);
214
        } else {
215
            $config_client_secret = config('mati')['client_secret'];
216
            if ($config_client_secret) {
217
                $this->setClientSecret($config_client_secret);
218
            }
219
        }
220
    }
221
}
222