ImporterService.php (4346B)
1 <?php 2 3 namespace PartKeepr\ImportBundle\Service; 4 5 use Doctrine\Bundle\DoctrineBundle\Registry; 6 use Doctrine\ORM\EntityManager; 7 use Doctrine\ORM\UnitOfWork; 8 use Dunglas\ApiBundle\Api\IriConverter; 9 use PartKeepr\DoctrineReflectionBundle\Filter\AdvancedSearchFilter; 10 use PartKeepr\DoctrineReflectionBundle\Services\ReflectionService; 11 use PartKeepr\ImportBundle\Configuration\EntityConfiguration; 12 use Symfony\Component\PropertyAccess\PropertyAccessor; 13 14 class ImporterService 15 { 16 /** @var EntityManager */ 17 protected $em; 18 19 protected $reflectionService; 20 21 protected $importData; 22 23 protected $baseEntity; 24 25 protected $importConfiguration; 26 27 protected $advancedSearchFilter; 28 29 protected $iriConverter; 30 31 public function __construct( 32 Registry $doctrine, 33 ReflectionService $reflectionService, 34 AdvancedSearchFilter $advancedSearchFilter, 35 IriConverter $iriConverter 36 ) { 37 $this->em = $doctrine->getManager(); 38 $this->reflectionService = $reflectionService; 39 $this->advancedSearchFilter = $advancedSearchFilter; 40 $this->iriConverter = $iriConverter; 41 } 42 43 /** 44 * @param mixed $baseEntity 45 */ 46 public function setBaseEntity($baseEntity) 47 { 48 $this->baseEntity = $this->reflectionService->convertExtJSToPHPClassName($baseEntity); 49 } 50 51 /** 52 * @param mixed $importConfiguration 53 */ 54 public function setImportConfiguration($importConfiguration) 55 { 56 $this->importConfiguration = $importConfiguration; 57 } 58 59 public function setImportData($importData) 60 { 61 $this->importData = $importData; 62 } 63 64 public function import($preview = false) 65 { 66 $entities = []; 67 $logs = []; 68 69 $configuration = $this->parseConfiguration(); 70 71 $this->em->beginTransaction(); 72 73 foreach ($this->importData as $row) { 74 $this->em->beginTransaction(); 75 $entity = $configuration->import($row); 76 77 if ($entity !== null) { 78 $entities[] = $entity; 79 } 80 $logs[] = implode( 81 "<br/>", 82 ["data" => implode(",", $row), '<p style="text-indent: 50px;">', "log" => " ".implode("<br/> ", $configuration->getLog()), '</p>'] 83 ); 84 85 $configuration->clearLog(); 86 87 foreach ($configuration->getPersistEntities() as $entity) { 88 $this->em->persist($entity); 89 } 90 91 $this->em->flush(); 92 $this->em->commit(); 93 } 94 95 if ($preview) { 96 $this->em->rollback(); 97 } else { 98 $this->em->commit(); 99 } 100 101 return [$configuration->getPersistEntities(), implode("<br/>", $logs)]; 102 } 103 104 public function parseConfiguration() 105 { 106 $cm = $this->em->getClassMetadata($this->baseEntity); 107 108 $configuration = new EntityConfiguration( 109 $cm, 110 $this->baseEntity, 111 $this->reflectionService, 112 $this->em, 113 $this->advancedSearchFilter, 114 $this->iriConverter 115 ); 116 $configuration->parseConfiguration($this->importConfiguration); 117 118 return $configuration; 119 } 120 121 public function describe($entity) 122 { 123 $accessor = new PropertyAccessor(); 124 125 $description = []; 126 127 switch ($this->em->getUnitOfWork()->getEntityState($entity)) { 128 case UnitOfWork::STATE_NEW: 129 $description["title"] = "Would create a new entity of type ".get_class($entity); 130 131 $cm = $this->em->getClassMetadata(get_class($entity)); 132 foreach ($cm->getFieldNames() as $fieldName) { 133 $description["fields"][$fieldName] = $accessor->getValue($entity, $fieldName); 134 } 135 136 foreach ($cm->getAssociationNames() as $associationMapping) { 137 $foo = $accessor->getValue($entity, $associationMapping); 138 139 if ($foo !== null) { 140 $description["associations"][$associationMapping] = $foo->getId(); 141 } else { 142 $description["error"] = "Would stop import because a mapping was not found"; 143 } 144 } 145 break; 146 } 147 148 $descriptions[] = $description; 149 150 return $description; 151 } 152 }