ApiKey::createPrimary()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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  string $otpCode
65
     * @return object
66
     */
67 1
    public function createPrimary(string $providerId, string $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 1
            $providerId,
74 1
            $otpCode
75
        );
76
77
        // get the response
78 1
        $response = $this->client->post($url, [
79 1
            'json' => [],
80
        ]);
81
82
        // return the response
83 1
        return $response;
84
    }
85
86
    /**
87
     * Sends an OTP code in an SMS to
88
     * the given mobile number.
89
     *
90
     * @param  array  $body
91
     * @return object
92
     */
93 1
    public function recoverPrimary(array $body)
94
    {
95
        // keys that are mandatory for this request
96 1
        $needKeys = ['MobileNumber', 'ProviderID'];
97
98
        // if the body doesn't have needed fields, throw an exception
99 1
        $this->validateHasKeys($body, $needKeys);
100
101 1
        if (isset($body['SmsMessage'])) {
102 1
            if (! preg_match('/\{0\}/', $body['SmsMessage'])) {
103 1
                throw new InvalidArgumentException('SmsMessage must contain a {0}');
104
            }
105
        }
106
107
        // make the URL for this request
108 1
        $url = sprintf('%s/OTP/RenewPrimaryKeyStep1', $this->getBaseUrl());
109
110
        // get the response
111 1
        $response = $this->client->put($url, [
112 1
            'json' => $body,
113
        ]);
114
115
        // return the response
116 1
        return $response;
117
    }
118
}
119