-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportContext.php
314 lines (248 loc) · 9.32 KB
/
ImportContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Import;
use Klipper\Component\Content\ContentManagerInterface;
use Klipper\Component\Import\Model\ImportInterface;
use Klipper\Component\Metadata\MetadataManagerInterface;
use Klipper\Component\Metadata\ObjectMetadataInterface;
use Klipper\Component\Resource\Domain\DomainInterface;
use Klipper\Component\Resource\Domain\DomainManagerInterface;
use Klipper\Component\Resource\ResourceInterface;
use Klipper\Component\Resource\ResourceStatutes;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use function Symfony\Component\String\b;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author François Pluchino <[email protected]>
*/
class ImportContext implements ImportContextInterface
{
private const DEFAULT_ROW_HEIGHT = 15;
private FormFactoryInterface $formFactory;
private TranslatorInterface $translator;
private DomainManagerInterface $domainManager;
private ContentManagerInterface $contentManager;
private MetadataManagerInterface $metadataManager;
private DomainInterface $domainImport;
private DomainInterface $domainTarget;
private ObjectMetadataInterface $metadataTarget;
private ImportInterface $import;
private array $mappingColumns;
private array $mappingFields;
private array $mappingAssociations;
private Spreadsheet $spreadsheet;
private Worksheet $activeSheet;
private IWriter $writer;
private string $file;
private PropertyAccessorInterface $propertyAccessor;
public function __construct(
FormFactoryInterface $formFactory,
TranslatorInterface $translator,
DomainManagerInterface $domainManager,
ContentManagerInterface $contentManager,
MetadataManagerInterface $metadataManager,
DomainInterface $domainImport,
DomainInterface $domainTarget,
ObjectMetadataInterface $metadataTarget,
ImportInterface $import,
array $mappingColumns,
array $mappingFields,
array $mappingAssociations,
Spreadsheet $spreadsheet,
Worksheet $activeSheet,
IWriter $writer,
string $file,
?PropertyAccessorInterface $propertyAccessor = null
) {
$this->formFactory = $formFactory;
$this->translator = $translator;
$this->domainManager = $domainManager;
$this->contentManager = $contentManager;
$this->metadataManager = $metadataManager;
$this->domainImport = $domainImport;
$this->domainTarget = $domainTarget;
$this->metadataTarget = $metadataTarget;
$this->import = $import;
$this->mappingColumns = $mappingColumns;
$this->mappingFields = $mappingFields;
$this->mappingAssociations = $mappingAssociations;
$this->spreadsheet = $spreadsheet;
$this->activeSheet = $activeSheet;
$this->writer = $writer;
$this->file = $file;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}
public function getFormFactory(): FormFactoryInterface
{
return $this->formFactory;
}
public function getTranslator(): TranslatorInterface
{
return $this->translator;
}
public function getPropertyAccessor(): PropertyAccessorInterface
{
return $this->propertyAccessor;
}
public function getDomainManager(): DomainManagerInterface
{
return $this->domainManager;
}
public function getContentManager(): ContentManagerInterface
{
return $this->contentManager;
}
public function getMetadataManager(): MetadataManagerInterface
{
return $this->metadataManager;
}
public function getDomainImport(): DomainInterface
{
return $this->domainImport;
}
public function getDomainTarget(): DomainInterface
{
return $this->domainTarget;
}
public function getMetadataTarget(): ObjectMetadataInterface
{
return $this->metadataTarget;
}
public function getImport(): ImportInterface
{
return $this->import;
}
public function getMappingColumns(): array
{
return $this->mappingColumns;
}
public function getMappingFields(): array
{
return $this->mappingFields;
}
public function getMappingAssociations(): array
{
return $this->mappingAssociations;
}
public function getSpreadsheet(): Spreadsheet
{
return $this->spreadsheet;
}
public function getActiveSheet(): Worksheet
{
return $this->activeSheet;
}
public function getWriter(): IWriter
{
return $this->writer;
}
public function getFile(): string
{
return $this->file;
}
public function getLocale(): string
{
return $this->import->getLocale() ?? 'en';
}
public function saveWriter(): void
{
$this->writer->save($this->getFile());
}
public function getFieldIdentifierIndex(): int
{
$fieldIdentifier = $this->metadataTarget->getFieldIdentifier();
return $this->mappingColumns[$fieldIdentifier] ?? \count($this->mappingColumns) + 1;
}
public function getImportStatusIndex(): int
{
return $this->mappingColumns['@import_status'] ?? \count($this->mappingColumns) + 2;
}
public function getImportMessageIndex(): int
{
return $this->mappingColumns['@import_message'] ?? \count($this->mappingColumns) + 2;
}
public function setResult(ResourceInterface $resource, int $rowIndex): void
{
$sheet = $this->getActiveSheet();
if ($resource->isValid()) {
$this->import->setSuccessCount($this->import->getSuccessCount() + 1);
} else {
$this->import->setErrorCount($this->import->getErrorCount() + 1);
}
// Inject Id
$id = $this->propertyAccessor->getValue($resource->getRealData(), $this->metadataTarget->getFieldIdentifier());
$sheet->setCellValueByColumnAndRow($this->getFieldIdentifierIndex(), $rowIndex, $id);
// Inject status
$sheet->setCellValueByColumnAndRow($this->getImportStatusIndex(), $rowIndex, $resource->getStatus());
// Inject message
$message = $resource->isValid() ? null : $this->buildErrors($resource);
$sheet->setCellValueByColumnAndRow($this->getImportMessageIndex(), $rowIndex, $message);
$rowDim = $sheet->getRowDimension($rowIndex);
$finalVal = $sheet->getCellByColumnAndRow($this->getImportMessageIndex(), $rowIndex)->getValue();
$lineHeight = self::DEFAULT_ROW_HEIGHT;
if (\is_string($finalVal)) {
$lineHeight = self::DEFAULT_ROW_HEIGHT * \count(b($finalVal)->split("\n"));
}
if ($lineHeight > $rowDim->getRowHeight()) {
$rowDim->setRowHeight($lineHeight);
}
}
public function setResultError(int $rowIndex, $message): void
{
$sheet = $this->getActiveSheet();
$statusColIndex = $this->getImportStatusIndex();
$messageColIndex = $this->getImportMessageIndex();
$sheet->setCellValueByColumnAndRow($statusColIndex, $rowIndex, ResourceStatutes::ERROR);
$sheet->setCellValueByColumnAndRow($messageColIndex, $rowIndex, $message);
$this->import->setErrorCount($this->import->getErrorCount() + 1);
}
public function saveImport(): ResourceInterface
{
return $this->domainImport->update($this->import);
}
/**
* Build the errors message.
*
* @param ResourceInterface $resource The resource
* @param int $indent The indentation
*/
private function buildErrors(ResourceInterface $resource, int $indent = 0): string
{
$formErrors = $resource->getFormErrors();
$rootErrors = $resource->getErrors();
$titleField = $this->translator->trans('klipper_import.title_field');
$indentStr = sprintf("%{$indent}s", '');
$message = PHP_EOL.$indentStr.$this->translator->trans('klipper_import.title_errors');
/** @var FormError $formError */
foreach ($formErrors as $formError) {
$message .= PHP_EOL.$indentStr.' - ';
if (null !== $formError->getOrigin()->getParent()) {
$message .= $titleField.' "'.$formError->getOrigin()->getName().'": ';
}
$message .= $formError->getMessage();
}
if ($rootErrors->count() > 0) {
/** @var ConstraintViolationInterface $rootError */
foreach ($rootErrors as $rootError) {
$message .= PHP_EOL.' '.$rootError->getMessage();
}
} elseif (0 === \count($formErrors)) {
$message .= PHP_EOL.' '.$this->translator->trans('klipper_import.error_without_message');
}
return trim($message);
}
}