|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sausin\Signere; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use UnexpectedValueException; |
|
7
|
|
|
|
|
8
|
|
|
class DocumentFile extends BaseClass |
|
9
|
|
|
{ |
|
10
|
|
|
/** The URI of the action */ |
|
11
|
|
|
const URI = 'https://api.signere.no/api/DocumentFile'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Returns the signed document as a file. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $documentId |
|
17
|
|
|
* @return object |
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function getSigned(string $documentId) |
|
20
|
|
|
{ |
|
21
|
|
|
// make the URL for this request |
|
22
|
1 |
|
$url = $this->transformUrl(sprintf('%s/Signed/%s', self::URI, $documentId)); |
|
23
|
|
|
|
|
24
|
|
|
// get the headers for this request |
|
25
|
1 |
|
$headers = $this->headers->make('GET', $url); |
|
26
|
|
|
|
|
27
|
|
|
// get the response |
|
28
|
1 |
|
$response = $this->client->get($url, [ |
|
29
|
1 |
|
'headers' => $headers, |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
|
|
// return the response |
|
33
|
1 |
|
return $response; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the unsigned document as a file. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $documentId |
|
40
|
|
|
* @return object |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function getUnSigned(string $documentId) |
|
43
|
|
|
{ |
|
44
|
|
|
// make the URL for this request |
|
45
|
1 |
|
$url = $this->transformUrl(sprintf('%s/Unsigned/%s', self::URI, $documentId)); |
|
46
|
|
|
|
|
47
|
|
|
// get the headers for this request |
|
48
|
1 |
|
$headers = $this->headers->make('GET', $url); |
|
49
|
|
|
|
|
50
|
|
|
// get the response |
|
51
|
1 |
|
$response = $this->client->get($url, [ |
|
52
|
1 |
|
'headers' => $headers, |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
// return the response |
|
56
|
1 |
|
return $response; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the signed document as a PDF file. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $documentId |
|
63
|
|
|
* @return object |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getSignedPdf(string $documentId) |
|
66
|
|
|
{ |
|
67
|
|
|
// make the URL for this request |
|
68
|
1 |
|
$url = $this->transformUrl(sprintf('%s/SignedPDF/%s', self::URI, $documentId)); |
|
69
|
|
|
|
|
70
|
|
|
// get the headers for this request |
|
71
|
1 |
|
$headers = $this->headers->make('GET', $url); |
|
72
|
|
|
|
|
73
|
|
|
// get the response |
|
74
|
1 |
|
$response = $this->client->get($url, [ |
|
75
|
1 |
|
'headers' => $headers, |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
// return the response |
|
79
|
1 |
|
return $response; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates a temporary url to a document. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $documentId |
|
86
|
|
|
* @param string $type |
|
87
|
|
|
* @param Carbon|null $expiring |
|
88
|
|
|
* @return object |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function temporaryUrl(string $documentId, string $type = 'PDF', Carbon $expiring = null) |
|
91
|
|
|
{ |
|
92
|
|
|
// the file types that can be accepted |
|
93
|
2 |
|
$needTypes = ['SDO', 'PDF', 'SIGNED_PDF', 'MOBILE_SDO', 'XML']; |
|
94
|
|
|
|
|
95
|
|
|
// check if specified type is correct |
|
96
|
2 |
|
if (! in_array($type, $needTypes, true)) { |
|
97
|
1 |
|
throw new UnexpectedValueException('File type should be one of '.implode(', ', $needTypes)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// make the URL for this request |
|
101
|
2 |
|
$url = $this->transformUrl(sprintf('%s/TempUrl', self::URI, $documentId)); |
|
102
|
|
|
|
|
103
|
|
|
// setup body |
|
104
|
2 |
|
$expiring = $expiring ?: Carbon::now()->addHours(48); |
|
105
|
|
|
$body = [ |
|
106
|
2 |
|
'DocumentId' => $documentId, |
|
107
|
2 |
|
'DocumentFileType' => $type, |
|
108
|
2 |
|
'Expires' => substr($expiring->setTimezone('UTC')->toIso8601String(), 0, 19), |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
// get the headers for this request |
|
112
|
2 |
|
$headers = $this->headers->make('POST', $url, $body); |
|
113
|
|
|
|
|
114
|
|
|
// get the response |
|
115
|
2 |
|
$response = $this->client->post($url, [ |
|
116
|
2 |
|
'headers' => $headers, |
|
117
|
2 |
|
'json' => $body, |
|
118
|
|
|
]); |
|
119
|
|
|
|
|
120
|
|
|
// return the response |
|
121
|
2 |
|
return $response; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|