src/AppBundle/EventListeners/UuidCalculator.php line 42

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListeners;
  3. use Pimcore;
  4. use Pimcore\Event\Model\ElementEventInterface;
  5. use Pimcore\Model\DataObject\Product;
  6. use Pimcore\Model\DataObject\Supplier;
  7. use Ramsey\Uuid\Uuid;
  8. use Ramsey\Uuid\UuidInterface;
  9. class UuidCalculator
  10. {
  11.     /**
  12.      * {@inheritdoc
  13.      */
  14.     public static function install()
  15.     {
  16.         return true;
  17.     }
  18.     /**
  19.      * {@inheritdoc
  20.      */
  21.     public static function uninstall()
  22.     {
  23.         return true;
  24.     }
  25.     /**
  26.      * {@inheritdoc
  27.      */
  28.     public static function isInstalled()
  29.     {
  30.         return true;
  31.     }
  32.     /**
  33.      * @param ElementEventInterface $e
  34.      * @throws \Exception
  35.      */
  36.     public function handleEvent(ElementEventInterface $e)
  37.     {
  38.         if (!$e instanceof Pimcore\Event\Model\DataObjectEvent) {
  39.             return;
  40.         }
  41.         $object $e->getObject();
  42.         if (!$object instanceof Product and !$object instanceof Supplier) {
  43.             return;
  44.         }
  45.         if (!method_exists($object'getUuid')) {
  46.             return;
  47.         }
  48.         if ($object->getUuid()) {
  49.             return;
  50.         }
  51.         $object->setUuid($this->generateUuid());
  52.     }
  53.     /**
  54.      * @return UuidInterface
  55.      * @throws \Exception
  56.      */
  57.     protected function generateUuid()
  58.     {
  59.         $uuid Uuid::uuid4();
  60.         return $uuid;
  61.     }
  62. }