<?php
declare(strict_types=1);
namespace Abcpremium\PrivatsortimentPriceImportBundle\Event\Subscriber;
use Abcpremium\PrivatsortimentPriceImportBundle\Model\Import\Processor;
use Abcpremium\PrivatsortimentPriceImportBundle\Model\Lock;
use Pimcore\Event\AdminEvents;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\ValidationException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class DataObjectEvents implements EventSubscriberInterface
{
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
\Pimcore\Event\DataObjectEvents::PRE_UPDATE => 'throwExceptionBeforeSaveIfLocked',
AdminEvents::OBJECT_GET_PRE_SEND_DATA => 'throwExceptionBeforeOpenIfLocked',
];
}
public function throwExceptionBeforeSaveIfLocked(DataObjectEvent $dataObjectEvent): void
{
$dataObject = $dataObjectEvent->getObject();
if (!$dataObject instanceof Concrete) {
return;
}
if ($dataObjectEvent->hasArgument(Processor::SAVE_ARGUMENT_NAME) && true === $dataObjectEvent->getArgument(Processor::SAVE_ARGUMENT_NAME)) {
return;
}
$this->throwExceptionIfLocked($dataObject);
}
public function throwExceptionBeforeOpenIfLocked(GenericEvent $event): void
{
/**
* @var $dataObject Concrete|AbstractObject
*/
$dataObject = $event->getArgument('object');
if (!$dataObject instanceof Concrete) {
return;
}
$this->throwExceptionIfLocked($dataObject);
}
private function throwExceptionIfLocked(Concrete $dataObject): void
{
if (false === Lock::isObjectIdLocked($dataObject->getId())) {
return;
}
throw new ValidationException('Object is locked due price update');
}
}