|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oc\FieldNotes\Import; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
|
|
|
|
|
6
|
|
|
use Oc\FieldNotes\Context\HandleFormContext; |
|
7
|
|
|
use Oc\FieldNotes\Exception\FileFormatException; |
|
8
|
|
|
use Oc\FieldNotes\Form\UploadFormData; |
|
9
|
|
|
use Oc\FieldNotes\Import\Context\ImportContext; |
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
11
|
|
|
use Oc\Validator\Exception\ValidationException; |
|
12
|
|
|
use Oc\Validator\Validator; |
|
13
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
14
|
|
|
use Symfony\Component\ExpressionLanguage\Node\Node; |
|
15
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
16
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ImportService |
|
20
|
|
|
* |
|
21
|
|
|
* @package Oc\FieldNotes\Import |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
|
|
class ImportService |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EntityManagerInterface |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
protected $entityManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var TranslatorInterface |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
|
|
protected $translator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FileParser |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
|
|
private $fileParser; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Validator |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
private $validator; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Importer |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
private $importer; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* ImportService constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Importer $importer |
|
|
|
|
|
|
54
|
|
|
* @param FileParser $fileParser |
|
|
|
|
|
|
55
|
|
|
* @param Validator $validator |
|
|
|
|
|
|
56
|
|
|
* @param TranslatorInterface $translator |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( |
|
59
|
|
|
Importer $importer, |
|
60
|
|
|
FileParser $fileParser, |
|
61
|
|
|
Validator $validator, |
|
62
|
|
|
TranslatorInterface $translator |
|
63
|
|
|
) { |
|
64
|
|
|
$this->translator = $translator; |
|
65
|
|
|
$this->fileParser = $fileParser; |
|
66
|
|
|
$this->validator = $validator; |
|
67
|
|
|
$this->importer = $importer; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Handles submitted form data. |
|
72
|
|
|
* |
|
73
|
|
|
* @param UploadFormData $formData |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @return HandleFormContext |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
public function handleFormData(UploadFormData $formData) |
|
78
|
|
|
{ |
|
79
|
|
|
$success = false; |
|
80
|
|
|
$errors = []; |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$fieldNotes = $this->fileParser->parseFile($formData->file); |
|
84
|
|
|
|
|
85
|
|
|
$this->validator->validate($fieldNotes); |
|
86
|
|
|
|
|
87
|
|
|
$context = new ImportContext($fieldNotes, $formData); |
|
88
|
|
|
|
|
89
|
|
|
$this->importer->import($context); |
|
90
|
|
|
|
|
91
|
|
|
$success = true; |
|
92
|
|
|
} catch (FileFormatException $e) { |
|
93
|
|
|
$errors[] = $this->translator->trans('field_notes.error.wrong_file_format'); |
|
94
|
|
|
} catch (ValidationException $e) { |
|
95
|
|
|
/** |
|
96
|
|
|
* @var ConstraintViolationInterface $violation |
|
|
|
|
|
|
97
|
|
|
*/ |
|
98
|
|
|
foreach ($e->getViolations() as $violation) { |
|
99
|
|
|
$linePrefix = $this->getTranslatedLinePrefix($violation); |
|
100
|
|
|
|
|
101
|
|
|
$errors[] = sprintf( |
|
102
|
|
|
'%s %s', |
|
103
|
|
|
$linePrefix, |
|
104
|
|
|
$violation->getMessage() |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} catch (Exception $e) { |
|
108
|
|
|
$errors[] = $this->translator->trans('general.error.unknown_error'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return new HandleFormContext($success, $errors); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Fetches the line of the constraint violation and returns the line prefix with line number. |
|
116
|
|
|
* |
|
117
|
|
|
* @param ConstraintViolationInterface $violation |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
private function getTranslatedLinePrefix(ConstraintViolationInterface $violation) |
|
122
|
|
|
{ |
|
123
|
|
|
/** |
|
124
|
|
|
* @var Node $expressionAst |
|
|
|
|
|
|
125
|
|
|
*/ |
|
126
|
|
|
$expressionAst = (new ExpressionLanguage())->parse($violation->getPropertyPath(), [])->getNodes(); |
|
127
|
|
|
|
|
128
|
|
|
$line = ((int)$expressionAst->nodes['node']->nodes[1]->attributes['value']) + 1; |
|
129
|
|
|
|
|
130
|
|
|
$linePrefix = $this->translator->trans('field_notes.error.line_prefix', [ |
|
131
|
|
|
'%line%' => $line |
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
|
|
return $linePrefix; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|