Completed
Push — master ( ad4a08...63fe60 )
by Saurabh
10:36 queued 09:06
created

ApiKey   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 18.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
dl 22
loc 122
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renewPrimary() 0 17 1
A renewSecondary() 0 17 1
B recoverPrimary() 0 31 4
A createPrimary() 22 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 = sprintf('%s/RenewPrimaryKey?OldPrimaryKey=%s', $this->getBaseUrl(), $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 = sprintf('%s/RenewSecondaryKey?OldSecondaryKey=%s', $this->getBaseUrl(), $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 View Code Duplication
    public function createPrimary(string $providerId, int $otpCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
69
    {
70
        // make the URL for this request
71 1
        $url = sprintf(
72 1
            '%s/OTP/RenewPrimaryKeyStep2/Provider/%s/OTPCode/%s',
73 1
            $this->getBaseUrl(),
74 1
            $providerId,
75 1
            $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 = sprintf('%s/OTP/RenewPrimaryKeyStep1', $this->getBaseUrl());
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