mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-23 07:04:58 +00:00
refactor: adjust TrackCreateUpdateSubscriber for strict types and improve user handling
- Add `readonly` class and typed method return values - Replace `LifecycleEventArgs` with specific `PrePersistEventArgs` and `PreUpdateEventArgs` - Simplify `DateTimeImmutable` instantiation - Enhance user retrieval using variable assignment for type-checking
This commit is contained in:
@@ -15,15 +15,17 @@ use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
|
||||
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Doctrine\Common\EventSubscriber;
|
||||
use Doctrine\ORM\Event\PrePersistEventArgs;
|
||||
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
class TrackCreateUpdateSubscriber implements EventSubscriber
|
||||
readonly class TrackCreateUpdateSubscriber implements EventSubscriber
|
||||
{
|
||||
public function __construct(private readonly Security $security) {}
|
||||
public function __construct(private Security $security) {}
|
||||
|
||||
public function getSubscribedEvents()
|
||||
public function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
Events::prePersist,
|
||||
@@ -31,22 +33,22 @@ class TrackCreateUpdateSubscriber implements EventSubscriber
|
||||
];
|
||||
}
|
||||
|
||||
public function prePersist(LifecycleEventArgs $args): void
|
||||
public function prePersist(PrePersistEventArgs $args): void
|
||||
{
|
||||
$object = $args->getObject();
|
||||
|
||||
if ($object instanceof TrackCreationInterface) {
|
||||
$object->setCreatedAt(new \DateTimeImmutable('now'));
|
||||
$object->setCreatedAt(new \DateTimeImmutable());
|
||||
|
||||
if ($this->security->getUser() instanceof User) {
|
||||
$object->setCreatedBy($this->security->getUser());
|
||||
if (($user = $this->security->getUser()) instanceof User) {
|
||||
$object->setCreatedBy($user);
|
||||
}
|
||||
}
|
||||
|
||||
$this->onUpdate($object);
|
||||
$this->onUpdate($object, $args);
|
||||
}
|
||||
|
||||
public function preUpdate(LifecycleEventArgs $args): void
|
||||
public function preUpdate(PreUpdateEventArgs $args): void
|
||||
{
|
||||
$object = $args->getObject();
|
||||
|
||||
@@ -56,10 +58,10 @@ class TrackCreateUpdateSubscriber implements EventSubscriber
|
||||
protected function onUpdate(object $object): void
|
||||
{
|
||||
if ($object instanceof TrackUpdateInterface) {
|
||||
$object->setUpdatedAt(new \DateTimeImmutable('now'));
|
||||
$object->setUpdatedAt(new \DateTimeImmutable());
|
||||
|
||||
if ($this->security->getUser() instanceof User) {
|
||||
$object->setUpdatedBy($this->security->getUser());
|
||||
if (($user = $this->security->getUser()) instanceof User) {
|
||||
$object->setUpdatedBy($user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user