Completed
Push — master ( e41c6a...6a7f08 )
by Saurabh
03:07
created

ApiKey::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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