mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-03 23:46:12 +00:00
66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Chill\MainBundle\Doctrine\Event;
|
|
|
|
use Chill\MainBundle\Entity\User;
|
|
use Doctrine\Common\EventSubscriber;
|
|
use Doctrine\ORM\Events;
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
|
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
|
|
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
|
|
class TrackCreateUpdateSubscriber implements EventSubscriber
|
|
{
|
|
private Security $security;
|
|
|
|
/**
|
|
* @param Security $security
|
|
*/
|
|
public function __construct(Security $security)
|
|
{
|
|
$this->security = $security;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function getSubscribedEvents()
|
|
{
|
|
return [
|
|
Events::prePersist,
|
|
Events::preUpdate
|
|
];
|
|
}
|
|
|
|
public function prePersist(LifecycleEventArgs $args): void
|
|
{
|
|
$object = $args->getObject();
|
|
|
|
if ($object instanceof TrackCreationInterface
|
|
&& $this->security->getUser() instanceof User) {
|
|
$object->setCreatedBy($this->security->getUser());
|
|
$object->setCreatedAt(new \DateTimeImmutable('now'));
|
|
}
|
|
|
|
$this->onUpdate($object);
|
|
}
|
|
|
|
public function preUpdate(LifecycleEventArgs $args): void
|
|
{
|
|
$object = $args->getObject();
|
|
|
|
$this->onUpdate($object);
|
|
}
|
|
|
|
protected function onUpdate(object $object): void
|
|
{
|
|
if ($object instanceof TrackUpdateInterface
|
|
&& $this->security->getUser() instanceof User) {
|
|
$object->setUpdatedBy($this->security->getUser());
|
|
$object->setUpdatedAt(new \DateTimeImmutable('now'));
|
|
}
|
|
}
|
|
}
|