src/Profile/EventSubscriber/ProfileColorSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Profile\EventSubscriber;
  3. use App\Profile\Model\Profile;
  4. use App\Profile\Service\ProfileColorNormalizer;
  5. use Pimcore\Event\DataObjectEvents;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ProfileColorSubscriber implements EventSubscriberInterface
  9. {
  10.   public function __construct(protected ProfileColorNormalizer $colorNormalizer)
  11.   {
  12.   }
  13.   public static function getSubscribedEvents()
  14.   {
  15.     return [
  16.         DataObjectEvents::PRE_UPDATE => 'onDataObjectPreUpdate',
  17.     ];
  18.   }
  19.   public function onDataObjectPreUpdate(DataObjectEvent $event)
  20.   {
  21.     $object $event->getObject();
  22.     $className = (new \ReflectionClass($object))->getShortName();
  23.     if (!in_array($className, ['Profile'])) {
  24.       return;
  25.     }
  26.     /**
  27.      * @var Profile
  28.      */
  29.     $profile $object;
  30.     $normalizedColor $this->colorNormalizer->normalize($profile->getColor());
  31.     $profile->setColor($normalizedColor);
  32.   }
  33. }