Completed
Push — master ( 40ad2e...94f126 )
by Saurabh
12:19 queued 10:48
created

Document   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 241
Duplicated Lines 19.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
dl 46
loc 241
ccs 70
cts 72
cp 0.9722
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 16 1
A getSignUrl() 0 21 1
A getTemporaryUrl() 0 20 1
B getList() 0 24 5
A cancel() 23 23 1
A changeDeadline() 23 23 1
B create() 0 53 4

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