partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

ManyToOneConfiguration.php (8081B)


      1 <?php
      2 
      3 namespace PartKeepr\ImportBundle\Configuration;
      4 
      5 use Doctrine\ORM\QueryBuilder;
      6 
      7 class ManyToOneConfiguration extends Configuration
      8 {
      9     const IMPORTBEHAVIOUR_DONTSET = "dontSet";
     10     const IMPORTBEHAVIOUR_ALWAYSSETTO = "alwaysSetTo";
     11     const IMPORTBEHAVIOUR_MATCHDATA = "matchData";
     12 
     13     const importBehaviours = [
     14         self::IMPORTBEHAVIOUR_DONTSET,
     15         self::IMPORTBEHAVIOUR_ALWAYSSETTO,
     16         self::IMPORTBEHAVIOUR_MATCHDATA,
     17     ];
     18 
     19     const UPDATEBEHAVIOUR_DONTUPDATE = "dontUpdate";
     20     const UPDATEBEHAVIOUR_UPDATEDATA = "updateData";
     21 
     22     const updateBehaviours = [self::UPDATEBEHAVIOUR_DONTUPDATE, self::UPDATEBEHAVIOUR_UPDATEDATA];
     23 
     24     const NOTFOUNDBEHAVIOUR_STOPIMPORT = "stopImport";
     25     const NOTFOUNDBEHAVIOUR_SETTOENTITY = "setToEntity";
     26     const NOTFOUNDBEHAVIOUR_CREATEENTITY = "createEntity";
     27 
     28     const notFoundBehaviours = [
     29         self::NOTFOUNDBEHAVIOUR_CREATEENTITY,
     30         self::NOTFOUNDBEHAVIOUR_SETTOENTITY,
     31         self::NOTFOUNDBEHAVIOUR_STOPIMPORT,
     32     ];
     33 
     34     protected $associationName;
     35 
     36     protected $importBehaviour;
     37 
     38     protected $matchers = [];
     39 
     40     protected $notFoundBehaviour;
     41 
     42     protected $updateBehaviour;
     43 
     44     protected $notFoundSetToEntity;
     45 
     46     protected $setToEntity;
     47 
     48     public function parseConfiguration($importConfiguration)
     49     {
     50         if (!property_exists($importConfiguration, "importBehaviour")) {
     51             return false;
     52 
     53             throw new \Exception("The key importBehaviour does not exist!");
     54         }
     55 
     56         if (!in_array($importConfiguration->importBehaviour, self::importBehaviours)) {
     57             throw new \Exception("The key importBehaviour contains an invalid value!");
     58         }
     59 
     60         $this->importBehaviour = $importConfiguration->importBehaviour;
     61 
     62         switch ($this->importBehaviour) {
     63             case self::IMPORTBEHAVIOUR_ALWAYSSETTO:
     64                 if (!property_exists($importConfiguration, "setToEntity")) {
     65                     throw new \Exception("The key setToEntity does not exist for mode alwaysSetTo!");
     66                 }
     67 
     68                 // @todo Check if setToEntity contains a valid value
     69                 $this->setToEntity = $importConfiguration->setToEntity;
     70                 break;
     71             case self::IMPORTBEHAVIOUR_MATCHDATA:
     72                 if (!property_exists($importConfiguration, "matchers")) {
     73                     throw new \Exception("No matchers defined");
     74                 }
     75 
     76                 if (!is_array($importConfiguration->matchers)) {
     77                     throw new \Exception("matchers must be an array");
     78                 }
     79 
     80                 foreach ($importConfiguration->matchers as $matcher) {
     81                     if (!property_exists($matcher, "matchField") || !property_exists(
     82                         $matcher,
     83                         "importField"
     84                     ) || $matcher->importField === ""
     85                     ) {
     86                         throw new \Exception("matcher configuration error");
     87                     }
     88                 }
     89 
     90                 $this->matchers = $importConfiguration->matchers;
     91 
     92                 if (!property_exists($importConfiguration, "updateBehaviour")) {
     93                     throw new \Exception("The key updateBehaviour does not exist for mode copyFrom!");
     94                 }
     95 
     96                 if (!in_array($importConfiguration->updateBehaviour, self::updateBehaviours)) {
     97                     throw new \Exception(sprintf("Invalid value for updateBehaviour: %s", $importConfiguration->updateBehaviour));
     98                 }
     99 
    100                 $this->updateBehaviour = $importConfiguration->updateBehaviour;
    101 
    102                 if (!property_exists($importConfiguration, "notFoundBehaviour")) {
    103                     throw new \Exception("The key notFoundBehaviour does not exist for mode copyFrom!");
    104                 }
    105 
    106                 if (!in_array($importConfiguration->notFoundBehaviour, self::notFoundBehaviours)) {
    107                     throw new \Exception("Invalid value for notFoundBehaviour");
    108                 }
    109 
    110                 $this->notFoundBehaviour = $importConfiguration->notFoundBehaviour;
    111 
    112                 if ($this->notFoundBehaviour == self::NOTFOUNDBEHAVIOUR_SETTOENTITY) {
    113                     if (!property_exists($importConfiguration, "notFoundSetToEntity")) {
    114                         throw new \Exception("The key notFoundSetToEntity does not exist for mode copyFrom!");
    115                     }
    116 
    117                     // @todo check if notFoundSetToEntity contains a valid entity
    118                     $this->notFoundSetToEntity = $importConfiguration->notFoundSetToEntity;
    119                 }
    120                 break;
    121             default:
    122                 break;
    123         }
    124 
    125         return parent::parseConfiguration($importConfiguration);
    126     }
    127 
    128     public function import($row, $obj = null)
    129     {
    130         $descriptions = [];
    131         switch ($this->importBehaviour) {
    132             case self::IMPORTBEHAVIOUR_ALWAYSSETTO:
    133                 $targetEntity = $this->iriConverter->getItemFromIri($this->setToEntity);
    134                 $this->log(sprintf("Set %s to %s#%s", $this->associationName, $this->baseEntity, $targetEntity->getId()));
    135 
    136                 return $targetEntity;
    137                 break;
    138             case self::IMPORTBEHAVIOUR_MATCHDATA:
    139                 $configuration = [];
    140 
    141                 foreach ($this->matchers as $matcher) {
    142                     $foo = new \stdClass();
    143                     $foo->property = $matcher->matchField;
    144                     $foo->operator = "=";
    145                     $foo->value = $row[$matcher->importField];
    146 
    147                     $descriptions[] = sprintf("%s = %s", $matcher->matchField, $row[$matcher->importField]);
    148                     $configuration[] = $foo;
    149                 }
    150 
    151                 $configuration = $this->advancedSearchFilter->extractConfiguration($configuration, []);
    152 
    153                 $filters = $configuration['filters'];
    154                 $sorters = $configuration['sorters'];
    155                 $qb = new QueryBuilder($this->em);
    156                 $qb->select("o")->from($this->baseEntity, "o");
    157 
    158                 $this->advancedSearchFilter->filter($qb, $filters, $sorters);
    159 
    160                 try {
    161                     $result = $qb->getQuery()->getSingleResult();
    162 
    163                     if ($this->updateBehaviour === self::UPDATEBEHAVIOUR_UPDATEDATA) {
    164                         // @todo Update the entity with the specified values
    165                     }
    166 
    167                     $this->log(sprintf("Set %s to %s#%s", $this->associationName, $this->baseEntity, $result->getId()));
    168 
    169                     return $result;
    170                 } catch (\Exception $e) {
    171                 }
    172 
    173                     switch ($this->notFoundBehaviour) {
    174                         case self::NOTFOUNDBEHAVIOUR_STOPIMPORT:
    175                             $this->log(sprintf("Stop import as the match %s for association %s was not found", implode(",", $descriptions), $this->getAssociationName()));
    176                             break;
    177                         case self::NOTFOUNDBEHAVIOUR_SETTOENTITY:
    178                             $targetEntity = $this->iriConverter->getItemFromIri($this->notFoundSetToEntity);
    179                             $this->log(sprintf("Set the association %s to %s, since the match %s for association %s was not found", $this->getAssociationName(), $this->notFoundSetToEntity, implode(",", $descriptions)));
    180 
    181                             return $targetEntity;
    182                             break;
    183                         case self::NOTFOUNDBEHAVIOUR_CREATEENTITY:
    184                             $this->log(sprintf("Create a new entity of type %s", $this->baseEntity));
    185 
    186                             return parent::import($row);
    187                             break;
    188 
    189                     }
    190 
    191                 break;
    192         }
    193 
    194         return null;
    195     }
    196 
    197     /**
    198      * @return mixed
    199      */
    200     public function getAssociationName()
    201     {
    202         return $this->associationName;
    203     }
    204 
    205     /**
    206      * @param mixed $associationName
    207      */
    208     public function setAssociationName($associationName)
    209     {
    210         $this->associationName = $associationName;
    211     }
    212 }