Completed
Push — master ( e41c6a...6a7f08 )
by Saurabh
03:07
created

Document::cancel()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 27
ccs 9
cts 9
cp 1
crap 2
rs 8.8571
c 0
b 0
f 0
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 = $this->transformUrl(sprintf(
22 1
            '%s/SignUrl?documentId=%s&signeeRefId=%s',
23 1
            self::URI,
24
            $documentId,
25
            $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 = $this->transformUrl(sprintf('%s/%s', self::URI, $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 = $this->transformUrl(sprintf(
74 1
            '%s/SignedDocument/TemporaryViewerUrl/%s',
75 1
            self::URI,
76
            $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 = $this->transformUrl(sprintf(
102 1
            '%s/?Status=%s&Fromdate=%s&JobId=%s&CreatedAfter=%s&ExternalCustomerRef=%s',
103 1
            self::URI,
104 1
            isset($params['status']) ? $params['status'] : 'All',
105 1
            isset($params['from_date']) ? $params['from_date'] : null,
106
            $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 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...
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
        if (! array_has_all_keys($body, $needKeys)) {
154
            throw new BadMethodCallException(
155
                'Missing fields in input array. Need '.implode(', ', $needKeys)
156
            );
157 1
        } elseif (! is_array($body['SigneeRefs'])) {
158
            throw new UnexpectedValueException('SigneeRefs key in input should be an array');
159
        } else {
160 1
            foreach ($body['SigneeRefs'] as $ref) {
161 1
                if (! is_array($ref)) {
162
                    throw new UnexpectedValueException('Each item in SigneeRefs should be an array');
163 1
                } elseif (! array_has_all_keys($ref, $needSubKeys)) {
164
                    throw new BadMethodCallException(
165 1
                        'Missing fields in SigneeRefs item. Need '.implode(', ', $needSubKeys)
166
                    );
167
                }
168
            }
169
        }
170
171
        // make the URL for this request
172 1
        $url = $this->transformUrl(self::URI);
173
174
        // get the headers for this request
175 1
        $headers = $this->headers->make('POST', $url, $body, true);
176
177
        // get the response
178 1
        $response = $this->client->post($url, [
179 1
            'headers' => $headers,
180 1
            'json' => $body,
181
        ]);
182
183
        // return the response
184 1
        return $response;
185
    }
186
187
    /**
188
     * Creates a new document to sign, and returns
189
     * a document response object.
190
     *
191
     * @param  array  $body
192
     * @return object
193
     */
194 1
    public function cancel(array $body)
195
    {
196
        // keys that are mandatory for this request
197 1
        $needKeys = ['CanceledDate', 'DocumentID', 'Signature'];
198
199
        // if the body doesn't have needed fields, throw an exception
200 1
        if (! array_has_all_keys($body, $needKeys)) {
201
            throw new BadMethodCallException(
202
                'Missing fields in input array. Need '.implode(', ', $needKeys)
203
            );
204
        }
205
206
        // make the URL for this request
207 1
        $url = $this->transformUrl(sprintf('%s/CancelDocument', self::URI));
208
209
        // get the headers for this request
210 1
        $headers = $this->headers->make('POST', $url, $body);
211
212
        // get the response
213 1
        $response = $this->client->post($url, [
214 1
            'headers' => $headers,
215 1
            'json' => $body,
216
        ]);
217
218
        // return the response
219 1
        return $response;
220
    }
221
222
    /**
223
     * Creates a new document to sign, and returns
224
     * a document response object.
225
     *
226
     * @param  array  $body
227
     * @return object
228
     */
229 1
    public function changeDeadline(array $body)
230
    {
231
        // keys that are mandatory for this request
232 1
        $needKeys = ['DocumentID', 'NewDeadline', 'ProviderID'];
233
234
        // if the body doesn't have needed fields, throw an exception
235 1
        if (! array_has_all_keys($body, $needKeys)) {
236
            throw new BadMethodCallException(
237
                'Missing fields in input array. Need '.implode(', ', $needKeys)
238
            );
239
        }
240
241
        // make the URL for this request
242 1
        $url = $this->transformUrl(sprintf('%s/ChangeDeadline', self::URI));
243
244
        // get the headers for this request
245 1
        $headers = $this->headers->make('PUT', $url, $body);
246
247
        // get the response
248 1
        $response = $this->client->put($url, [
249 1
            'headers' => $headers,
250 1
            'json' => $body,
251
        ]);
252
253
        // return the response
254 1
        return $response;
255
    }
256
}
257