Completed
Push — master ( 365164...87d4f7 )
by Saurabh
08:29 queued 30s
created

ApiKey::createPrimary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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