src/AppBundle/EventListeners/ObjectValidator.php line 20

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListeners;
  3. use Pimcore;
  4. use Pimcore\Event\Model\ElementEventInterface;
  5. use Pimcore\Model\DataObject\AbstractObject;
  6. use Pimcore\Model\DataObject\Country;
  7. use Pimcore\Model\DataObject\Product;
  8. use Pimcore\Model\Element\ValidationException;
  9. use Website\Validators\CountryValidator;
  10. use Website\Validators\ProductValidator;
  11. class ObjectValidator
  12. {
  13.     /**
  14.      * @param ElementEventInterface $e
  15.      * @throws ValidationException
  16.      */
  17.     public function handleEvent(ElementEventInterface $e)
  18.     {
  19.         if (!$e instanceof Pimcore\Event\Model\DataObjectEvent) {
  20.             return;
  21.         }
  22.         $object $e->getObject();
  23.         if ($object->getType() === AbstractObject::OBJECT_TYPE_FOLDER || !$object instanceof Pimcore\Model\DataObject\Concrete) {
  24.             return;
  25.         }
  26.         if (!$object->isPublished()) {
  27.             return;
  28.         }
  29.         if ($object instanceof Product) {
  30.             $validator = new ProductValidator();
  31.         } elseif ($object instanceof Country) {
  32.             $validator = new CountryValidator();
  33.         } else {
  34.             return;
  35.         }
  36.         if ($validator->hasErrors($object)) {
  37.             throw new ValidationException(implode(" "$validator->getErrors()));
  38.         }
  39.     }
  40. }