for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Dontdrinkandroot\Event\Listener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Dontdrinkandroot\Entity\UuidEntityInterface;
use Ramsey\Uuid\Uuid;
/**
* @author Philip Washington Sorst <[email protected]>
*/
class UuidEntityListener
{
public function prePersist(LifecycleEventArgs $args): void
$entity = $args->getEntity();
if (is_a($entity, UuidEntityInterface::class)) {
/** @var UuidEntityInterface $uuidEntity */
$uuidEntity = $entity;
if (null === $uuidEntity->getUuid()) {
$uuidEntity->setUuid(Uuid::uuid4()->toString());
}