Completed
Push — master ( d50eef...e43aa5 )
by Saurabh
04:55 queued 03:19
created

ApiKey   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 139
Duplicated Lines 15.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 22
loc 139
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

5 Methods

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