<?php
namespace AppBundle\EventListeners;
use Pimcore;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\Supplier;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
class UuidCalculator
{
/**
* {@inheritdoc
*/
public static function install()
{
return true;
}
/**
* {@inheritdoc
*/
public static function uninstall()
{
return true;
}
/**
* {@inheritdoc
*/
public static function isInstalled()
{
return true;
}
/**
* @param ElementEventInterface $e
* @throws \Exception
*/
public function handleEvent(ElementEventInterface $e)
{
if (!$e instanceof Pimcore\Event\Model\DataObjectEvent) {
return;
}
$object = $e->getObject();
if (!$object instanceof Product and !$object instanceof Supplier) {
return;
}
if (!method_exists($object, 'getUuid')) {
return;
}
if ($object->getUuid()) {
return;
}
$object->setUuid($this->generateUuid());
}
/**
* @return UuidInterface
* @throws \Exception
*/
protected function generateUuid()
{
$uuid = Uuid::uuid4();
return $uuid;
}
}