EntityConfiguration.php (5725B)
1 <?php 2 3 namespace PartKeepr\ImportBundle\Configuration; 4 5 use Doctrine\ORM\QueryBuilder; 6 7 class EntityConfiguration extends Configuration 8 { 9 const IMPORTBEHAVIOUR_ALWAYSIMPORT = "alwaysImport"; 10 const IMPORTBEHAVIOUR_MATCHDATA = "matchData"; 11 12 const importBehaviours = [ 13 self::IMPORTBEHAVIOUR_ALWAYSIMPORT, 14 self::IMPORTBEHAVIOUR_MATCHDATA, 15 ]; 16 17 const UPDATEBEHAVIOUR_DONTUPDATE = "dontUpdate"; 18 const UPDATEBEHAVIOUR_UPDATEDATA = "update"; 19 20 const updateBehaviours = [self::UPDATEBEHAVIOUR_DONTUPDATE, self::UPDATEBEHAVIOUR_UPDATEDATA]; 21 22 protected $importBehaviour; 23 24 protected $updateBehaviour; 25 26 protected $matchers = []; 27 28 public function parseConfiguration($importConfiguration) 29 { 30 if (!property_exists($importConfiguration, "importBehaviour")) { 31 throw new \Exception(sprintf("The key importBehaviour does not exist for path /%s!", implode("/", $this->getPath()))); 32 } 33 34 if (!in_array($importConfiguration->importBehaviour, self::importBehaviours)) { 35 throw new \Exception("The key importBehaviour contains an invalid value!"); 36 } 37 38 $this->importBehaviour = $importConfiguration->importBehaviour; 39 40 switch ($this->importBehaviour) { 41 case self::IMPORTBEHAVIOUR_MATCHDATA: 42 if (!property_exists($importConfiguration, "matchers")) { 43 throw new \Exception("No matchers defined"); 44 } 45 46 if (!is_array($importConfiguration->matchers)) { 47 throw new \Exception("matchers must be an array"); 48 } 49 50 foreach ($importConfiguration->matchers as $matcher) { 51 if (!property_exists($matcher, "matchField") || !property_exists( 52 $matcher, 53 "importField" 54 ) || $matcher->importField === "" 55 ) { 56 throw new \Exception("matcher configuration error"); 57 } 58 } 59 60 $this->matchers = $importConfiguration->matchers; 61 62 if (!property_exists($importConfiguration, "updateBehaviour")) { 63 throw new \Exception("The key updateBehaviour does not exist for mode matchData!"); 64 } 65 66 if (!in_array($importConfiguration->updateBehaviour, self::updateBehaviours)) { 67 throw new \Exception("Invalid value for updateBehaviour"); 68 } 69 70 $this->updateBehaviour = $importConfiguration->updateBehaviour; 71 72 break; 73 default: 74 break; 75 } 76 77 return parent::parseConfiguration($importConfiguration); 78 } 79 80 public function import($row, $obj = null) 81 { 82 $descriptions = []; 83 switch ($this->importBehaviour) { 84 case self::IMPORTBEHAVIOUR_ALWAYSIMPORT: 85 $obj = new $this->baseEntity(); 86 $this->persist($obj); 87 88 parent::import($row, $obj); 89 break; 90 case self::IMPORTBEHAVIOUR_MATCHDATA: 91 $configuration = []; 92 93 foreach ($this->matchers as $matcher) { 94 $foo = new \stdClass(); 95 $foo->property = $matcher->matchField; 96 $foo->operator = "="; 97 $foo->value = $row[$matcher->importField]; 98 99 $descriptions[] = sprintf("%s = %s", $matcher->matchField, $row[$matcher->importField]); 100 $configuration[] = $foo; 101 } 102 103 $configuration = $this->advancedSearchFilter->extractConfiguration($configuration, []); 104 105 $filters = $configuration['filters']; 106 $sorters = $configuration['sorters']; 107 $qb = new QueryBuilder($this->em); 108 $qb->select("o")->from($this->baseEntity, "o"); 109 110 $this->advancedSearchFilter->filter($qb, $filters, $sorters); 111 112 try { 113 $result = $qb->getQuery()->getArrayResult(); 114 115 if (count($result) === 0) { 116 $this->log( 117 sprintf( 118 "No item of type %s for the configured matcher (%s) found, creating a new one", 119 $this->baseEntity, 120 implode($descriptions, ", ") 121 ) 122 ); 123 124 return parent::import($row); 125 } 126 127 if (count($result) === 1) { 128 $this->log( 129 sprintf( 130 "Found item of type %s for the configured matcher (%s)", 131 $this->baseEntity, 132 implode($descriptions, ", ") 133 ) 134 ); 135 136 return parent::import($row, $result[0]); 137 } 138 139 if (count($result) > 1) { 140 $this->log( 141 sprintf( 142 "Found %d items of type %s for the configured matcher (%s). Can't continue since we don't know which item to use. Configure the matcher to narrow the results", 143 count($result), 144 $this->baseEntity, 145 implode($descriptions, ", ") 146 ) 147 ); 148 149 return null; 150 } 151 } catch (\Exception $e) { 152 } 153 } 154 155 return null; 156 } 157 }