Completed
Pull Request — master (#6)
by Saurabh
10:00 queued 08:24
created

DocumentProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 187
Duplicated Lines 21.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.07%

Importance

Changes 0
Metric Value
dl 41
loc 187
ccs 51
cts 56
cp 0.9107
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProviderAccount() 0 16 1
A getCertExpiry() 0 16 1
B create() 10 44 4
B update() 10 32 4
A getUsage() 21 21 2

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 UnexpectedValueException;
8
9
class DocumentProvider
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/DocumentProvider';
19
20
    /**
21
     * Instantiate the class.
22
     *
23
     * @param \GuzzleHttp\Client $client
24
     */
25 6
    public function __construct(Client $client, Headers $headers)
26
    {
27 6
        $this->client = $client;
28 6
        $this->headers = $headers;
29 6
    }
30
31
    /**
32
     * Retrieves a document provider account.
33
     *
34
     * @param  string $providerId
35
     * @return object
36
     */
37 1
    public function getProviderAccount(string $providerId)
38
    {
39
        // make the URL for this request
40 1
        $url = sprintf('%s/%s', self::URI, $providerId);
41
42
        // get the headers for this request
43 1
        $headers = $this->headers->make('GET', $url);
44
45
        // get the response
46 1
        $response = $this->client->get($url, [
47 1
            'headers' => $headers,
48
        ]);
49
50
        // return the response
51 1
        return $response;
52
    }
53
54
    /**
55
     * Gets the expires date for your BankID certificate. If you don't
56
     * have your own BankID certificate it will return Bad request.
57
     *
58
     * @return object
59
     */
60 1
    public function getCertExpiry()
61
    {
62
        // make the URL for this request
63 1
        $url = sprintf('%s/CertificateExpires', self::URI);
64
65
        // get the headers for this request
66 1
        $headers = $this->headers->make('GET', $url);
67
68
        // get the response
69 1
        $response = $this->client->get($url, [
70 1
            'headers' => $headers,
71
        ]);
72
73
        // return the response
74 1
        return $response;
75
    }
76
77
    /**
78
     * Get the usage when using prepaid or demo account.
79
     *
80
     * @param  string       $providerId
81
     * @param  bool|bool $demo
82
     * @return object
83
     */
84 2 View Code Duplication
    public function getUsage(string $providerId, bool $demo = false)
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...
85
    {
86
        // make the URL for this request
87 2
        $url = sprintf(
88 2
            '%s/quota/%s?ProviderId=%s',
89 2
            self::URI,
90 2
            $demo ? 'demo' : 'prepaid',
91 2
            $providerId
92
        );
93
94
        // get the headers for this request
95 2
        $headers = $this->headers->make('GET', $url);
96
97
        // get the response
98 2
        $response = $this->client->get($url, [
99 2
            'headers' => $headers,
100
        ]);
101
102
        // return the response
103 2
        return $response;
104
    }
105
106
    /**
107
     * Creates a new document provider.
108
     *
109
     * @param  array  $body
110
     * @return object
111
     */
112 1
    public function create(array $body)
113
    {
114
        // keys that are mandatory for this request
115
        $needKeys = [
116 1
            'BillingAddress1',
117
            'BillingCity',
118
            'BillingPostalCode',
119
            'CompanyEmail',
120
            'CompanyPhone',
121
            'DealerId',
122
            'LegalContactEmail',
123
            'LegalContactName',
124
            'LegalContactPhone',
125
            'MvaNumber',
126
            'Name',
127
        ];
128
129
        // if the body doesn't have needed fields, throw an exception
130 1 View Code Duplication
        if (! array_has_all_keys($body, $needKeys)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131
            throw new BadMethodCallException(
132
                'Missing fields in input array. Need '.implode(', ', $needKeys)
133
            );
134 1
        } elseif (isset($body['BillingPlan'])) {
135 1
            $expected = ['Small', 'Medium', 'Large'];
136 1
            if (! in_array($body['BillingPlan'], $expected)) {
137 1
                throw new UnexpectedValueException('BillingPlan should be one of '.implode(', ', $expected));
138
            }
139
        }
140
141
        // make the URL for this request
142 1
        $url = self::URI;
143
144
        // get the headers for this request
145 1
        $headers = $this->headers->make('POST', $url, $body);
146
147
        // get the response
148 1
        $response = $this->client->post($url, [
149 1
            'headers' => $headers,
150 1
            'json' => $body,
151
        ]);
152
153
        // return the response
154 1
        return $response;
155
    }
156
157
    /**
158
     * Updates a new document provider.
159
     *
160
     * @param  array  $body
161
     * @return object
162
     */
163 1
    public function update(array $body)
164
    {
165
        // keys that are mandatory for this request
166 1
        $needKeys = ['Mobile', 'ProviderId'];
167
168
        // if the body doesn't have needed fields, throw an exception
169 1 View Code Duplication
        if (! array_has_all_keys($body, $needKeys)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
170 1
            throw new BadMethodCallException(
171 1
                'Missing fields in input array. Need '.implode(', ', $needKeys)
172
            );
173 1
        } elseif (isset($body['BillingPlan'])) {
174
            $expected = ['Small', 'Medium', 'Large'];
175
            if (! in_array($body['BillingPlan'], $expected)) {
176
                throw new UnexpectedValueException('BillingPlan should be one of '.implode(', ', $expected));
177
            }
178
        }
179
180
        // make the URL for this request
181 1
        $url = self::URI;
182
183
        // get the headers for this request
184 1
        $headers = $this->headers->make('PUT', $url, $body);
185
186
        // get the response
187 1
        $response = $this->client->put($url, [
188 1
            'headers' => $headers,
189 1
            'json' => $body,
190
        ]);
191
192
        // return the response
193 1
        return $response;
194
    }
195
}
196