<?php
namespace AppBundle\EventListeners;
use Pimcore;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Country;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\Element\ValidationException;
use Website\Validators\CountryValidator;
use Website\Validators\ProductValidator;
class ObjectValidator
{
/**
* @param ElementEventInterface $e
* @throws ValidationException
*/
public function handleEvent(ElementEventInterface $e)
{
if (!$e instanceof Pimcore\Event\Model\DataObjectEvent) {
return;
}
$object = $e->getObject();
if ($object->getType() === AbstractObject::OBJECT_TYPE_FOLDER || !$object instanceof Pimcore\Model\DataObject\Concrete) {
return;
}
if (!$object->isPublished()) {
return;
}
if ($object instanceof Product) {
$validator = new ProductValidator();
} elseif ($object instanceof Country) {
$validator = new CountryValidator();
} else {
return;
}
if ($validator->hasErrors($object)) {
throw new ValidationException(implode(" ", $validator->getErrors()));
}
}
}