Passed
Pull Request — development (#682)
by Nick
07:18
created

ImportService::handleFormData()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 13
nop 1
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\FieldNotes\Import;
4
5
use Exception;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
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
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
22
 */
23
class ImportService
24
{
25
    /**
26
     * @var EntityManagerInterface
0 ignored issues
show
introduced by
EntityManagerInterface => \Doctrine\ORM\EntityManagerInterface
Loading history...
27
     */
28
    protected $entityManager;
29
30
    /**
31
     * @var TranslatorInterface
0 ignored issues
show
introduced by
TranslatorInterface => \Symfony\Component\Translation\TranslatorInterface
Loading history...
32
     */
33
    protected $translator;
34
35
    /**
36
     * @var FileParser
0 ignored issues
show
introduced by
FileParser => \Array\FileParser
Loading history...
37
     */
38
    private $fileParser;
39
40
    /**
41
     * @var Validator
0 ignored issues
show
introduced by
Validator => \Oc\Validator\Validator
Loading history...
42
     */
43
    private $validator;
44
45
    /**
46
     * @var Importer
0 ignored issues
show
introduced by
Importer => \Array\Importer
Loading history...
47
     */
48
    private $importer;
49
50
    /**
51
     * ImportService constructor.
52
     *
53
     * @param Importer $importer
0 ignored issues
show
introduced by
Importer => \Array\Importer
Loading history...
54
     * @param FileParser $fileParser
0 ignored issues
show
introduced by
FileParser => \Array\FileParser
Loading history...
55
     * @param Validator $validator
0 ignored issues
show
introduced by
Validator => \Oc\Validator\Validator
Loading history...
56
     * @param TranslatorInterface $translator
0 ignored issues
show
introduced by
TranslatorInterface => \Symfony\Component\Translation\TranslatorInterface
Loading history...
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
0 ignored issues
show
introduced by
UploadFormData => \Oc\FieldNotes\Form\UploadFormData
Loading history...
74
     *
75
     * @return HandleFormContext
0 ignored issues
show
introduced by
HandleFormContext => \Oc\FieldNotes\Context\HandleFormContext
Loading history...
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
0 ignored issues
show
introduced by
ConstraintViolationInterface => \Symfony\Component\Validator\ConstraintViolationInterface
Loading history...
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
0 ignored issues
show
introduced by
ConstraintViolationInterface => \Symfony\Component\Validator\ConstraintViolationInterface
Loading history...
118
     *
119
     * @return string
120
     */
121
    private function getTranslatedLinePrefix(ConstraintViolationInterface $violation)
122
    {
123
        /**
124
         * @var Node $expressionAst
0 ignored issues
show
introduced by
Node => \Symfony\Component\ExpressionLanguage\Node\Node
Loading history...
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