2019-04-11 22:41:21 +02:00
|
|
|
<?php
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-04-11 22:41:21 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-04-11 22:41:21 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
2020-02-01 19:42:28 +01:00
|
|
|
use Symfony\Bundle\MakerBundle\Str;
|
2020-01-05 22:49:00 +01:00
|
|
|
use function count;
|
2019-04-11 22:41:21 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use function is_array;
|
2019-04-11 22:41:21 +02:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
|
|
|
class EntityImporter
|
|
|
|
{
|
|
|
|
protected $serializer;
|
|
|
|
protected $em;
|
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
public function __construct(SerializerInterface $serializer, EntityManagerInterface $em, ValidatorInterface $validator)
|
|
|
|
{
|
|
|
|
$this->serializer = $serializer;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->validator = $validator;
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:41:58 +02:00
|
|
|
/**
|
2019-11-09 16:14:57 +01:00
|
|
|
* Creates many entries at once, based on a (text) list of name.
|
|
|
|
* The created enties are not persisted to database yet, so you have to do it yourself.
|
2019-08-12 22:41:58 +02:00
|
|
|
*
|
2019-11-09 00:47:20 +01:00
|
|
|
* @param string $lines The list of names seperated by \n
|
|
|
|
* @param string $class_name The name of the class for which the entities should be created
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractStructuralDBElement|null $parent the element which will be used as parent element for new elements
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param array $errors an associative array containing all validation errors
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-02-01 19:48:07 +01:00
|
|
|
* @return AbstractStructuralDBElement[] An array containing all valid imported entities (with the type $class_name)
|
2019-08-12 22:41:58 +02:00
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function massCreation(string $lines, string $class_name, ?AbstractStructuralDBElement $parent = null, array &$errors = []): array
|
2019-08-12 22:41:58 +02:00
|
|
|
{
|
|
|
|
//Expand every line to a single entry:
|
|
|
|
$names = explode("\n", $lines);
|
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
if (! is_a($class_name, AbstractStructuralDBElement::class, true)) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$class_name must be a StructuralDBElement type!');
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
2020-01-05 15:46:58 +01:00
|
|
|
if (null !== $parent && ! is_a($parent, $class_name)) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$parent must have the same type as specified in $class_name!');
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$errors = [];
|
2019-11-09 16:14:57 +01:00
|
|
|
$valid_entities = [];
|
2019-08-12 22:41:58 +02:00
|
|
|
|
|
|
|
foreach ($names as $name) {
|
|
|
|
$name = trim($name);
|
2020-01-04 20:24:09 +01:00
|
|
|
if ('' === $name) {
|
2019-11-09 16:14:57 +01:00
|
|
|
//Skip empty lines (StrucuralDBElements must have a name)
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-01 19:48:07 +01:00
|
|
|
/** @var AbstractStructuralDBElement $entity */
|
2019-08-12 22:41:58 +02:00
|
|
|
//Create new element with given name
|
|
|
|
$entity = new $class_name();
|
|
|
|
$entity->setName($name);
|
|
|
|
$entity->setParent($parent);
|
|
|
|
|
|
|
|
//Validate entity
|
|
|
|
$tmp = $this->validator->validate($entity);
|
|
|
|
//If no error occured, write entry to DB:
|
2020-01-05 22:49:00 +01:00
|
|
|
if (0 === count($tmp)) {
|
2019-11-09 16:14:57 +01:00
|
|
|
$valid_entities[] = $entity;
|
2019-08-12 22:41:58 +02:00
|
|
|
} else { //Otherwise log error
|
2020-01-05 22:49:00 +01:00
|
|
|
$errors[] = [
|
|
|
|
'entity' => $entity,
|
|
|
|
'violations' => $tmp,
|
|
|
|
];
|
2019-08-12 22:41:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 16:14:57 +01:00
|
|
|
return $valid_entities;
|
2019-08-12 22:41:58 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
/**
|
|
|
|
* This methods deserializes the given file and saves it database.
|
|
|
|
* The imported elements will be checked (validated) before written to database.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param File $file the file that should be used for importing
|
|
|
|
* @param string $class_name the class name of the enitity that should be imported
|
|
|
|
* @param array $options options for the import process
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-04-11 22:41:21 +02:00
|
|
|
* @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
|
2019-11-09 00:47:20 +01:00
|
|
|
* if an error happened during validation. When everything was successfull, the array should be empty.
|
2019-04-11 22:41:21 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function fileToDBEntities(File $file, string $class_name, array $options = []): array
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
|
|
|
$entities = $this->fileToEntityArray($file, $class_name, $options);
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$errors = [];
|
2019-04-11 22:41:21 +02:00
|
|
|
|
|
|
|
//Iterate over each $entity write it to DB.
|
|
|
|
foreach ($entities as $entity) {
|
2020-02-01 19:48:07 +01:00
|
|
|
/** @var AbstractStructuralDBElement $entity */
|
2019-04-11 22:41:21 +02:00
|
|
|
//Move every imported entity to the selected parent
|
|
|
|
$entity->setParent($options['parent']);
|
|
|
|
|
|
|
|
//Validate entity
|
|
|
|
$tmp = $this->validator->validate($entity);
|
|
|
|
|
|
|
|
//When no validation error occured, persist entity to database (cascade must be set in entity)
|
2020-02-01 19:42:28 +01:00
|
|
|
if (empty($tmp)) {
|
2019-04-11 22:41:21 +02:00
|
|
|
$this->em->persist($entity);
|
|
|
|
} else { //Log validation errors to global log.
|
|
|
|
$errors[$entity->getFullPath()] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save changes to database, when no error happened, or we should continue on error.
|
2020-01-05 15:46:58 +01:00
|
|
|
if (empty($errors) || false === $options['abort_on_validation_error']) {
|
2019-04-11 22:41:21 +02:00
|
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method converts (deserialize) a (uploaded) file to an array of entities with the given class.
|
|
|
|
*
|
|
|
|
* The imported elements will NOT be validated. If you want to use the result array, you have to validate it by yourself.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param File $file the file that should be used for importing
|
|
|
|
* @param string $class_name the class name of the enitity that should be imported
|
|
|
|
* @param array $options options for the import process
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @return array an array containing the deserialized elements
|
2019-04-11 22:41:21 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function fileToEntityArray(File $file, string $class_name, array $options = []): array
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
|
|
|
//Read file contents
|
|
|
|
$content = file_get_contents($file->getRealPath());
|
|
|
|
|
|
|
|
$groups = ['simple'];
|
|
|
|
//Add group when the children should be preserved
|
|
|
|
if ($options['preserve_children']) {
|
|
|
|
$groups[] = 'include_children';
|
|
|
|
}
|
|
|
|
|
|
|
|
//The [] behind class_name denotes that we expect an array.
|
2019-11-09 00:47:20 +01:00
|
|
|
$entities = $this->serializer->deserialize($content, $class_name.'[]', $options['format'],
|
2020-01-05 22:49:00 +01:00
|
|
|
[
|
|
|
|
'groups' => $groups,
|
|
|
|
'csv_delimiter' => $options['csv_separator'],
|
|
|
|
]);
|
2019-04-11 22:41:21 +02:00
|
|
|
|
|
|
|
//Ensure we have an array of entitity elements.
|
2020-01-05 22:49:00 +01:00
|
|
|
if (! is_array($entities)) {
|
2019-04-11 22:41:21 +02:00
|
|
|
$entities = [$entities];
|
|
|
|
}
|
|
|
|
|
|
|
|
//The serializer has only set the children attributes. We also have to change the parent value (the real value in DB)
|
2020-02-01 19:48:07 +01:00
|
|
|
if ($entities[0] instanceof AbstractStructuralDBElement) {
|
2019-04-11 22:41:21 +02:00
|
|
|
$this->correctParentEntites($entities, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
protected function configureOptions(OptionsResolver $resolver): void
|
|
|
|
{
|
|
|
|
$resolver->setDefaults([
|
|
|
|
'csv_separator' => ';',
|
|
|
|
'format' => 'json',
|
|
|
|
'preserve_children' => true,
|
|
|
|
'parent' => null,
|
|
|
|
'abort_on_validation_error' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
/**
|
|
|
|
* This functions corrects the parent setting based on the children value of the parent.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param iterable $entities the list of entities that should be fixed
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param null|AbstractStructuralDBElement $parent the parent, to which the entity should be set
|
2019-04-11 22:41:21 +02:00
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
protected function correctParentEntites(iterable $entities, $parent = null): void
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
|
|
|
foreach ($entities as $entity) {
|
2020-02-01 19:48:07 +01:00
|
|
|
/** @var AbstractStructuralDBElement $entity */
|
2019-04-11 22:41:21 +02:00
|
|
|
$entity->setParent($parent);
|
|
|
|
//Do the same for the children of entity
|
|
|
|
$this->correctParentEntites($entity->getChildren(), $entity);
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|