Completed
Push — master ( 96dc87...3752b6 )
by Saurabh
12:32 queued 10:52
created

Document   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 260
Duplicated Lines 33.85 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.16%

Importance

Changes 0
Metric Value
dl 88
loc 260
ccs 67
cts 76
cp 0.8816
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSignUrl() 0 16 1
B getList() 0 24 5
B create() 56 56 6
B cancel() 0 27 2
B changeDeadline() 0 27 2
A get() 16 16 1
A getTemporaryUrl() 16 16 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
7
class Document
8
{
9
    /** @var \GuzzleHttp\Client */
10
    protected $client;
11
12
    /** @var Headers */
13
    protected $headers;
14
15
    /** The URI of the action */
16
    const URI = 'https://api.signere.no/api/Document';
17
18
    /**
19
     * Instantiate the class.
20
     *
21
     * @param \GuzzleHttp\Client $client
22
     */
23 7
    public function __construct(Client $client, Headers $headers)
24
    {
25 7
        $this->client = $client;
26 7
        $this->headers = $headers;
27 7
    }
28
29
    /**
30
     * Returns the url to sign the document for the given Signeeref
31
     * or the first Signeeref if not SigneerefId is specified.
32
     *
33
     * @param  string      $documentId
34
     * @param  string|null $signeeRefId
35
     * @return object
36
     */
37 1
    public function getSignUrl(string $documentId, string $signeeRefId = null)
38
    {
39
        // make the URL for this request
40 1
        $url = sprintf('%s/SignUrl?documentId=%s&signeeRefId=%s', self::URI, $documentId, $signeeRefId);
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
     * Retrieves the document with the given ID.
56
     *
57
     * @param  string      $documentId
58
     * @return object
59
     */
60 1 View Code Duplication
    public function get(string $documentId)
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...
61
    {
62
        // make the URL for this request
63 1
        $url = sprintf('%s/%s', self::URI, $documentId);
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
     * Returns a temporary URL for viewing a signed
79
     * document in the BankID applet.
80
     *
81
     * @param  string      $documentId
82
     * @return object
83
     */
84 1 View Code Duplication
    public function getTemporaryUrl(string $documentId)
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 1
        $url = sprintf('%s/SignedDocument/TemporaryViewerUrl/%s', self::URI, $documentId);
88
89
        // get the headers for this request
90 1
        $headers = $this->headers->make('GET', $url);
91
92
        // get the response
93 1
        $response = $this->client->get($url, [
94 1
            'headers' => $headers,
95
        ]);
96
97
        // return the response
98 1
        return $response;
99
    }
100
101
    /**
102
     * Retrieves a list of documents based on the given parameters.
103
     *
104
     * @param  string|null $jobId
105
     * @param  array       $params
106
     * @return object
107
     */
108 1
    public function getList(string $jobId = null, array $params = [])
109
    {
110
        // make the URL for this request
111 1
        $url = sprintf(
112 1
            '%s/?Status=%s&Fromdate=%s&JobId=%s&CreatedAfter=%s&ExternalCustomerRef=%s',
113 1
            self::URI,
114 1
            isset($params['status']) ? $params['status'] : 'All',
115 1
            isset($params['from_date']) ? $params['from_date'] : null,
116
            $jobId,
117 1
            isset($params['created_after']) ? $params['created_after'] : null,
118 1
            isset($params['ext_cust_ref']) ? $params['ext_cust_ref'] : null
119
        );
120
121
        // get the headers for this request
122 1
        $headers = $this->headers->make('GET', $url);
123
124
        // get the response
125 1
        $response = $this->client->get($url, [
126 1
            'headers' => $headers,
127
        ]);
128
129
        // return the response
130 1
        return $response;
131
    }
132
133
    /**
134
     * Creates a new document to sign, and returns
135
     * a document response object.
136
     *
137
     * @param  array  $body
138
     * @return object
139
     */
140 1 View Code Duplication
    public function create(array $body)
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...
141
    {
142
        // keys that are mandatory for this request
143
        $needKeys = [
144 1
            'Description',
145
            'FileContent',
146
            'FileMD5CheckSum',
147
            'FileName',
148
            'Language',
149
            'SigneeRefs',
150
            'SignJobId',
151
            'Title',
152
        ];
153
154
        // keys that need to be present in each signeeref
155
        $needSubKeys = [
156 1
            'SigneeRefId',
157
            'FirstName',
158
            'LastName',
159
            'Email',
160
        ];
161
162
        // if the body doesn't have needed fields, throw an exception
163 1
        if (! array_has_all_keys($body, $needKeys)) {
164
            throw new BadMethodCallException(
165
                'Missing fields in input array. Need '.implode(', ', $needKeys)
166
            );
167 1
        } elseif (! is_array($body['SigneeRefs'])) {
168
            throw new UnexpectedValueException('SigneeRefs key in input should be an array');
169
        } else {
170 1
            foreach ($body['SigneeRefs'] as $ref) {
171 1
                if (! is_array($ref)) {
172
                    throw new UnexpectedValueException('Each item in SigneeRefs should be an array');
173 1
                } elseif (! array_has_all_keys($ref, $needSubKeys)) {
174
                    throw new BadMethodCallException(
175 1
                        'Missing fields in SigneeRefs item. Need '.implode(', ', $needSubKeys)
176
                    );
177
                }
178
            }
179
        }
180
181
        // make the URL for this request
182 1
        $url = self::URI;
183
184
        // get the headers for this request
185 1
        $headers = $this->headers->make('POST', $url, $body, true);
186
187
        // get the response
188 1
        $response = $this->client->post($url, [
189 1
            'headers' => $headers,
190 1
            'json' => $body,
191
        ]);
192
193
        // return the response
194 1
        return $response;
195
    }
196
197
    /**
198
     * Creates a new document to sign, and returns
199
     * a document response object.
200
     *
201
     * @param  array  $body
202
     * @return object
203
     */
204 1
    public function cancel(array $body)
205
    {
206
        // keys that are mandatory for this request
207 1
        $needKeys = ['CanceledDate', 'DocumentID', 'Signature'];
208
209
        // if the body doesn't have needed fields, throw an exception
210 1
        if (! array_has_all_keys($body, $needKeys)) {
211
            throw new BadMethodCallException(
212
                'Missing fields in input array. Need '.implode(', ', $needKeys)
213
            );
214
        }
215
216
        // make the URL for this request
217 1
        $url = sprintf('%s/CancelDocument', self::URI);
218
219
        // get the headers for this request
220 1
        $headers = $this->headers->make('POST', $url, $body);
221
222
        // get the response
223 1
        $response = $this->client->post($url, [
224 1
            'headers' => $headers,
225 1
            'json' => $body,
226
        ]);
227
228
        // return the response
229 1
        return $response;
230
    }
231
232
    /**
233
     * Creates a new document to sign, and returns
234
     * a document response object.
235
     *
236
     * @param  array  $body
237
     * @return object
238
     */
239 1
    public function changeDeadline(array $body)
240
    {
241
        // keys that are mandatory for this request
242 1
        $needKeys = ['DocumentID', 'NewDeadline', 'ProviderID'];
243
244
        // if the body doesn't have needed fields, throw an exception
245 1
        if (! array_has_all_keys($body, $needKeys)) {
246
            throw new BadMethodCallException(
247
                'Missing fields in input array. Need '.implode(', ', $needKeys)
248
            );
249
        }
250
251
        // make the URL for this request
252 1
        $url = sprintf('%s/ChangeDeadline', self::URI);
253
254
        // get the headers for this request
255 1
        $headers = $this->headers->make('PUT', $url, $body);
256
257
        // get the response
258 1
        $response = $this->client->put($url, [
259 1
            'headers' => $headers,
260 1
            'json' => $body,
261
        ]);
262
263
        // return the response
264 1
        return $response;
265
    }
266
}
267