Completed
Push — master ( 51be17...87e6f6 )
by Jan
03:52
created

EntityImporter::massCreation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 3
dl 0
loc 30
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services;
33
34
35
use App\Entity\Base\NamedDBElement;
36
use App\Entity\Base\StructuralDBElement;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Symfony\Bundle\MakerBundle\Str;
39
use Symfony\Component\HttpFoundation\File\File;
40
use Symfony\Component\OptionsResolver\OptionsResolver;
41
use Symfony\Component\Serializer\SerializerInterface;
42
use Symfony\Component\Validator\Validator\ValidatorInterface;
43
44
class EntityImporter
45
{
46
47
    protected $serializer;
48
    protected $em;
49
    protected $validator;
50
51
    public function __construct(SerializerInterface $serializer, EntityManagerInterface $em, ValidatorInterface $validator)
52
    {
53
        $this->serializer = $serializer;
54
        $this->em = $em;
55
        $this->validator = $validator;
56
    }
57
58
    protected function configureOptions(OptionsResolver $resolver)
59
    {
60
        $resolver->setDefaults([
61
           'csv_separator' => ';',
62
            'format' => 'json',
63
            'preserve_children' => true,
64
            'parent' => null,
65
            'abort_on_validation_error' => true
66
        ]);
67
    }
68
69
    /**
70
     * Creates many entries at once, based on a (text) list of names.
71
     *
72
     * @param string $lines The list of names seperated by \n
73
     * @param string $class_name The name of the class for which the entities should be created
74
     * @param StructuralDBElement|null $parent The element which will be used as parent element for new elements.
75
     * @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
76
     * if an error happened during validation. When everything was successfull, the array should be empty.
77
     */
78
    public function massCreation(string $lines, string $class_name, ?StructuralDBElement $parent) : array
79
    {
80
        //Expand every line to a single entry:
81
        $names = explode("\n", $lines);
82
83
        $errors = array();
84
85
        foreach ($names as $name) {
86
            $name = trim($name);
87
            /** @var $entity StructuralDBElement */
88
            //Create new element with given name
89
            $entity = new $class_name();
90
            $entity->setName($name);
91
            $entity->setParent($parent);
92
93
            //Validate entity
94
            $tmp = $this->validator->validate($entity);
95
            //If no error occured, write entry to DB:
96
            if (count($tmp) === 0) {
97
                $this->em->persist($entity);
98
            } else { //Otherwise log error
99
                dump($tmp);
100
                $errors[$entity->getFullPath()] = $tmp;
101
            }
102
        }
103
104
        //Save changes to database
105
        $this->em->flush();
106
107
        return $errors;
108
    }
109
110
    /**
111
     * This methods deserializes the given file and saves it database.
112
     * The imported elements will be checked (validated) before written to database.
113
     * @param File $file The file that should be used for importing.
114
     * @param string $class_name The class name of the enitity that should be imported.
115
     * @param array $options Options for the import process.
116
     * @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
117
     * if an error happened during validation. When everything was successfull, the array should be empty.
118
     */
119
    public function fileToDBEntities(File $file, string $class_name, array $options = []) : array
120
    {
121
        $resolver = new OptionsResolver();
122
        $this->configureOptions($resolver);
123
124
        $options = $resolver->resolve($options);
125
126
127
        $entities = $this->fileToEntityArray($file, $class_name, $options);
128
129
        $errors = array();
130
131
        //Iterate over each $entity write it to DB.
132
        foreach ($entities as $entity) {
133
            /** @var StructuralDBElement $entity */
134
            //Move every imported entity to the selected parent
135
            $entity->setParent($options['parent']);
136
137
            //Validate entity
138
            $tmp = $this->validator->validate($entity);
139
140
            //When no validation error occured, persist entity to database (cascade must be set in entity)
141
            if (count($errors) === 0) {
142
                $this->em->persist($entity);
143
            } else { //Log validation errors to global log.
144
                $errors[$entity->getFullPath()] = $tmp;
145
            }
146
        }
147
148
        //Save changes to database, when no error happened, or we should continue on error.
149
        if (empty($errors) || $options['abort_on_validation_error'] == false) {
150
            $this->em->flush();
151
        }
152
153
        return $errors;
154
    }
155
156
    /**
157
     * This method converts (deserialize) a (uploaded) file to an array of entities with the given class.
158
     *
159
     * The imported elements will NOT be validated. If you want to use the result array, you have to validate it by yourself.
160
     * @param File $file The file that should be used for importing.
161
     * @param string $class_name The class name of the enitity that should be imported.
162
     * @param array $options Options for the import process.
163
     * @return array An array containing the deserialized elements.
164
     */
165
    public function fileToEntityArray(File $file, string $class_name, array $options = []) : array
166
    {
167
        $resolver = new OptionsResolver();
168
        $this->configureOptions($resolver);
169
170
        $options = $resolver->resolve($options);
171
172
        //Read file contents
173
        $content = file_get_contents($file->getRealPath());
174
175
        $groups = ['simple'];
176
        //Add group when the children should be preserved
177
        if ($options['preserve_children']) {
178
            $groups[] = 'include_children';
179
        }
180
181
        //The [] behind class_name denotes that we expect an array.
182
        $entities = $this->serializer->deserialize($content, $class_name . '[]', $options['format'],
183
            ['groups' => $groups, 'csv_delimiter' => $options['csv_separator']]);
184
185
        //Ensure we have an array of entitity elements.
186
        if(!is_array($entities)) {
0 ignored issues
show
introduced by
The condition is_array($entities) is always false.
Loading history...
187
            $entities = [$entities];
188
        }
189
190
        //The serializer has only set the children attributes. We also have to change the parent value (the real value in DB)
191
        if ($entities[0] instanceof StructuralDBElement) {
192
            $this->correctParentEntites($entities, null);
193
        }
194
195
        return $entities;
196
    }
197
198
    /**
199
     * This functions corrects the parent setting based on the children value of the parent.
200
     * @param iterable $entities The list of entities that should be fixed.
201
     * @param null $parent The parent, to which the entity should be set.
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $parent is correct as it would always require null to be passed?
Loading history...
202
     */
203
    protected function correctParentEntites(iterable $entities, $parent = null)
204
    {
205
        foreach ($entities as $entity) {
206
            /** @var $entity StructuralDBElement */
207
            $entity->setParent($parent);
208
            //Do the same for the children of entity
209
            $this->correctParentEntites($entity->getChildren(), $entity);
210
        }
211
    }
212
}