Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

ImportService::getTranslatedLinePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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