src/EventSubscriber/RefrigerantGasSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Exception;
  4. use Pimcore\Event\DataObjectEvents;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Pimcore\Model\DataObject\PropertyIdentification;
  7. use Pimcore\Model\DataObject\RefrigerantGas;
  8. use Pimcore\Model\DataObject\Service;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class RefrigerantGasSubscriber implements EventSubscriberInterface
  11. {
  12.     const REFRIGERANT_GAS_FOLDER '1 – Property Description/1.7 Refrigerant Gas';
  13.     /**
  14.      * @inheritDoc
  15.      */
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             DataObjectEvents::PRE_ADD => ['onPreAdd'10],
  20.             DataObjectEvents::PRE_UPDATE => ['onPreUpdate'10],
  21.         ];
  22.     }
  23.     /**
  24.      * @throws Exception
  25.      */
  26.     public function onPreAdd(DataObjectEvent $event): void
  27.     {
  28.         $refrigerantGas $event->getObject();
  29.         if (!($refrigerantGas instanceof RefrigerantGas)) {
  30.             return;
  31.         }
  32.         $refrigerantGas->setRFGAS_LocationName($refrigerantGas->getKey());
  33.         $propertyIdentification $this->getPropertyIdentification($refrigerantGas);
  34.         if (!empty($propertyIdentification)) {
  35.             $refrigerantGas->setRFGAS_RelatedProperty([$propertyIdentification]);
  36.         }
  37.     }
  38.     /**
  39.      * @throws Exception
  40.      */
  41.     public function onPreUpdate(DataObjectEvent $event): void
  42.     {
  43.         $refrigerantGas $event->getObject();
  44.         if (!($refrigerantGas instanceof RefrigerantGas)) {
  45.             return;
  46.         }
  47.         $refrigerantGas->setKey($refrigerantGas->getRFGAS_LocationName());
  48.         if (!empty($refrigerantGas->getRFGAS_RelatedProperty())) {
  49.             $propertyIdentification $refrigerantGas->getRFGAS_RelatedProperty()[0];
  50.             $parentFolder Service::createFolderByPath(
  51.                 $propertyIdentification->getFullPath() . DIRECTORY_SEPARATOR . static::REFRIGERANT_GAS_FOLDER
  52.             );
  53.             if (!empty($parentFolder)) {
  54.                 $refrigerantGas->setParent($parentFolder);
  55.             }
  56.         }
  57.     }
  58.     /**
  59.      * Go up the tree to find the center
  60.      * @param RefrigerantGas $refrigerantGas
  61.      * @return PropertyIdentification|null
  62.      */
  63.     private function getPropertyIdentification(RefrigerantGas $refrigerantGas): ?PropertyIdentification
  64.     {
  65.         $propertyIdentification $refrigerantGas->getParent();
  66.         $count 0;
  67.         while ($propertyIdentification && $count 7) {
  68.             /**
  69.              * Condition $count < 7 : guard to avoid infinite loop
  70.              */
  71.             if ($propertyIdentification instanceof PropertyIdentification) {
  72.                 break;
  73.             }
  74.             $propertyIdentification $propertyIdentification->getParent();
  75.             $count++;
  76.         }
  77.         return $propertyIdentification;
  78.     }
  79. }