1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of datamolino client. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 cwd.at GmbH <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cwd\Datamolino\Endpoints; |
15
|
|
|
|
16
|
|
|
use Cwd\Datamolino\Model\Document; |
17
|
|
|
use Cwd\Datamolino\Model\DocumentFile; |
18
|
|
|
use Cwd\Datamolino\Model\OriginalFile; |
19
|
|
|
use Cwd\Datamolino\Model\UploadFile; |
20
|
|
|
use Symfony\Component\Finder\Finder; |
21
|
|
|
use Symfony\Component\HttpFoundation\File\File as FileInfo; |
22
|
|
|
|
23
|
|
|
class DocumentEndpoint extends AbstractEndpoint |
24
|
|
|
{ |
25
|
|
|
public const PAYLOAD_LIMIT = 1024 * 1024 * 20; // 20MB |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string|Finder $finder Location of Files or Finder Instance |
29
|
|
|
* @param int $agendaId |
30
|
|
|
* @param string $type |
31
|
|
|
* @param bool $fileSplit |
32
|
|
|
* @param bool $lacyLoad |
33
|
|
|
* |
34
|
|
|
* @return Document[] |
35
|
|
|
* |
36
|
|
|
* @throws \Http\Client\Exception |
37
|
|
|
*/ |
38
|
|
|
public function createByFinder($finder, int $agendaId, string $type = Document::DOCTYPE_PURCHASE, $fileSplit = false, $lacyLoad = false): array |
39
|
|
|
{ |
40
|
|
|
$currentFileSize = 0; |
41
|
|
|
$resultDocuments = []; |
42
|
|
|
$documentFiles = []; |
43
|
|
|
|
44
|
|
|
if (!$finder instanceof Finder) { |
45
|
|
|
$finder = (new Finder())->in($finder); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($finder as $file) { |
49
|
|
|
$documentFile = new DocumentFile(); |
50
|
|
|
$documentFile->setType($type) |
51
|
|
|
->setAgendaId($agendaId) |
52
|
|
|
->setFileSplit($fileSplit); |
53
|
|
|
|
54
|
|
|
$mimeType = (new FileInfo($file->getPathname()))->getMimeType(); |
55
|
|
|
$currentFileSize += $file->getSize(); |
56
|
|
|
$documentFile->setFile( |
57
|
|
|
new UploadFile($file->getFilename(), $mimeType, $file->getContents()) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$documentFiles[] = $documentFile; |
61
|
|
|
|
62
|
|
|
if ($currentFileSize >= self::PAYLOAD_LIMIT) { |
63
|
|
|
$resultDocuments += $this->send($documentFiles, $lacyLoad); |
64
|
|
|
$documentFiles = []; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (count($documentFiles) > 0) { |
69
|
|
|
$resultDocuments += $this->send($documentFiles, $lacyLoad); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $resultDocuments; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $fileUri |
77
|
|
|
* @param int $agendaId |
78
|
|
|
* @param string $filename |
79
|
|
|
* @param string $type |
80
|
|
|
* @param bool $fileSplit |
81
|
|
|
* @param bool $lacyLoad |
82
|
|
|
* |
83
|
|
|
* @return Document |
84
|
|
|
* |
85
|
|
|
* @throws \Http\Client\Exception |
86
|
|
|
*/ |
87
|
|
|
public function create($fileUri, $agendaId, $filename, $type = Document::DOCTYPE_PURCHASE, $fileSplit = false, $lacyLoad = false): Document |
88
|
|
|
{ |
89
|
|
|
$file = new FileInfo($fileUri); |
90
|
|
|
$mimeType = $file->getMimeType(); |
91
|
|
|
|
92
|
|
|
$documentFile = new DocumentFile(); |
93
|
|
|
$documentFile->setType($type) |
94
|
|
|
->setAgendaId($agendaId) |
95
|
|
|
->setFileSplit($fileSplit) |
96
|
|
|
->setFile( |
97
|
|
|
new UploadFile($file->getFilename(), $mimeType, file_get_contents($file->getPathname())) |
98
|
|
|
) |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
return current($this->send([$documentFile], $lacyLoad)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param DocumentFile[] $files |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
* |
109
|
|
|
* @throws \Http\Client\Exception |
110
|
|
|
*/ |
111
|
|
|
public function send(array $files, $lacyLoad = true) |
112
|
|
|
{ |
113
|
|
|
$payload = $this->getClient()->getSerializer()->serialize(['documents' => $files], 'json'); |
114
|
|
|
$documents = $this->getClient()->call($payload, null, 'documents', Document::class, true, 'POST'); |
115
|
|
|
|
116
|
|
|
if ($lacyLoad) { |
117
|
|
|
$ids = []; |
118
|
|
|
/** @var Document $document */ |
119
|
|
|
foreach ($documents as $document) { |
120
|
|
|
$ids[] = $document->getId(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->find(current($files)->getAgendaId(), $ids); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $documents; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function get(int $id): Document |
130
|
|
|
{ |
131
|
|
|
return $this->getClient()->call(null, $id, 'docuemnts', Document::class, false, 'GET'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $document |
136
|
|
|
* |
137
|
|
|
* @return OriginalFile |
138
|
|
|
* |
139
|
|
|
* @throws \Http\Client\Exception |
140
|
|
|
*/ |
141
|
|
|
public function getOriginalFile($document): OriginalFile |
142
|
|
|
{ |
143
|
|
|
if ($document instanceof Document) { |
144
|
|
|
$document = $document->getId(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->getClient()->call(null, $document, 'documents', OriginalFile::class, false, 'GET', '/original_file'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int|Document $document |
152
|
|
|
* |
153
|
|
|
* @throws \Http\Client\Exception |
154
|
|
|
*/ |
155
|
|
|
public function delete($document): void |
156
|
|
|
{ |
157
|
|
|
if ($document instanceof Document) { |
158
|
|
|
$document = $document->getId(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->getClient()->call(null, $document, 'documents', null, false, 'DELETE'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param Document|int $document |
166
|
|
|
* @param string $text |
167
|
|
|
* |
168
|
|
|
* @throws \Http\Client\Exception |
169
|
|
|
*/ |
170
|
|
|
public function repair($document, $text): void |
171
|
|
|
{ |
172
|
|
|
if ($document instanceof Document) { |
173
|
|
|
$document = $document->getId(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->getClient()->call(null, $document, 'documents', OriginalFile::class, false, 'POST', sprintf( |
177
|
|
|
'/repair?repair_description=%s', urlencode($text)) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param int $agendaId |
183
|
|
|
* @param array $ids |
184
|
|
|
* @param \DateTime|null $modifiedSince |
185
|
|
|
* @param array $states |
186
|
|
|
* @param int $page |
187
|
|
|
* @param string $type |
188
|
|
|
* |
189
|
|
|
* @return mixed |
190
|
|
|
* |
191
|
|
|
* @throws \Http\Client\Exception |
192
|
|
|
*/ |
193
|
|
|
public function find($agendaId, array $ids = [], ?\DateTime $modifiedSince = null, array $states = [], $page = 1, $type = Document::DOCTYPE_PURCHASE) |
194
|
|
|
{ |
195
|
|
|
$queryString = [ |
196
|
|
|
sprintf('agenda_id=%s', $agendaId), |
197
|
|
|
sprintf('page=%s', $page), |
198
|
|
|
sprintf('type=%s', $type), |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
if (null !== $modifiedSince) { |
202
|
|
|
$queryString[] = sprintf('modified_since=%s', $modifiedSince->format('Y-m-d\TH:i:s\Z')); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if (count($states) > 0) { |
206
|
|
|
foreach ($states as $state) { |
207
|
|
|
$queryString[] = sprintf('state[]=%s', $state); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (count($ids) > 0) { |
212
|
|
|
foreach ($ids as $id) { |
213
|
|
|
$queryString[] = sprintf('ids[]=%s', $id); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->getClient()->call(null, sprintf('?%s', implode('&', $queryString)), 'documents', Document::class, true, 'GET'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|