1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\Signere; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class ApiKey extends BaseClass |
8
|
|
|
{ |
9
|
|
|
/** The URI of the action */ |
10
|
|
|
const URI = 'https://api.signere.no/api/ApiToken'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Renews the primary API key. |
14
|
|
|
* |
15
|
|
|
* @param string $key |
16
|
|
|
* @return object |
17
|
|
|
*/ |
18
|
1 |
|
public function renewPrimary(string $key) |
19
|
|
|
{ |
20
|
|
|
// make the URL for this request |
21
|
1 |
|
$url = sprintf('%s/RenewPrimaryKey?OldPrimaryKey=%s', $this->getBaseUrl(), $key); |
22
|
|
|
|
23
|
|
|
// get the headers for this request |
24
|
1 |
|
$headers = $this->headers->make('POST', $url, [], true); |
25
|
|
|
|
26
|
|
|
// get the response |
27
|
1 |
|
$response = $this->client->post($url, [ |
28
|
1 |
|
'headers' => $headers, |
29
|
|
|
'json' => [], |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
// return the response |
33
|
1 |
|
return $response; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Renews the secondary API key. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* @return object |
41
|
|
|
*/ |
42
|
1 |
|
public function renewSecondary(string $key) |
43
|
|
|
{ |
44
|
|
|
// make the URL for this request |
45
|
1 |
|
$url = sprintf('%s/RenewSecondaryKey?OldSecondaryKey=%s', $this->getBaseUrl(), $key); |
46
|
|
|
|
47
|
|
|
// get the headers for this request |
48
|
1 |
|
$headers = $this->headers->make('POST', $url, [], true); |
49
|
|
|
|
50
|
|
|
// get the response |
51
|
1 |
|
$response = $this->client->post($url, [ |
52
|
1 |
|
'headers' => $headers, |
53
|
|
|
'json' => [], |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
// return the response |
57
|
1 |
|
return $response; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Generate a new primary key and return it. |
62
|
|
|
* |
63
|
|
|
* @param string $providerId |
64
|
|
|
* @param int $otpCode |
65
|
|
|
* @return object |
66
|
|
|
*/ |
67
|
1 |
View Code Duplication |
public function createPrimary(string $providerId, int $otpCode) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
// make the URL for this request |
70
|
1 |
|
$url = sprintf( |
71
|
1 |
|
'%s/OTP/RenewPrimaryKeyStep2/Provider/%s/OTPCode/%s', |
72
|
1 |
|
$this->getBaseUrl(), |
73
|
|
|
$providerId, |
74
|
|
|
$otpCode |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
// get the headers for this request |
78
|
1 |
|
$headers = $this->headers->make('POST', $url, [], null); |
79
|
|
|
|
80
|
|
|
// get the response |
81
|
1 |
|
$response = $this->client->post($url, [ |
82
|
1 |
|
'headers' => $headers, |
83
|
|
|
'json' => [], |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
// return the response |
87
|
1 |
|
return $response; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sends an OTP code in an SMS to |
92
|
|
|
* the given mobile number. |
93
|
|
|
* |
94
|
|
|
* @param array $body |
95
|
|
|
* @return object |
96
|
|
|
*/ |
97
|
1 |
|
public function recoverPrimary(array $body) |
98
|
|
|
{ |
99
|
|
|
// keys that are mandatory for this request |
100
|
1 |
|
$needKeys = ['MobileNumber', 'ProviderID']; |
101
|
|
|
|
102
|
|
|
// if the body doesn't have needed fields, throw an exception |
103
|
1 |
|
$this->validateHasKeys($body, $needKeys); |
104
|
|
|
|
105
|
1 |
|
if (isset($body['SmsMessage'])) { |
106
|
1 |
|
if (! preg_match('/\{0\}/', $body['SmsMessage'])) { |
107
|
1 |
|
throw new InvalidArgumentException('SmsMessage must contain a {0}'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// make the URL for this request |
112
|
1 |
|
$url = sprintf('%s/OTP/RenewPrimaryKeyStep1', $this->getBaseUrl()); |
113
|
|
|
|
114
|
|
|
// get the headers for this request |
115
|
1 |
|
$headers = $this->headers->make('PUT', $url, $body, false); |
116
|
|
|
|
117
|
|
|
// get the response |
118
|
1 |
|
$response = $this->client->put($url, [ |
119
|
1 |
|
'headers' => $headers, |
120
|
1 |
|
'json' => $body, |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
// return the response |
124
|
1 |
|
return $response; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.