mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
add route for comment + track update/creation of entities
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Doctrine\Event;
|
||||
|
||||
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) {
|
||||
$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) {
|
||||
$object->setUpdatedBy($this->security->getUser());
|
||||
$object->setUpdatedAt(new \DateTimeImmutable('now'));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user