Completed
Push — master ( 365164...87d4f7 )
by Saurabh
08:29 queued 30s
created

Document::changeDeadline()   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.024

Importance

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