Passed
Pull Request — master (#216)
by Charly
05:48
created

CommentEventSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Fixtures\Event;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Doctrine\ORM\Events;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Zenstruck\Foundry\Tests\Fixtures\Entity\Comment;
9
10
class CommentEventSubscriber implements EventSubscriberInterface
11
{
12
    public const COMMENT_BODY = 'test-event';
13
    public const NEW_COMMENT_BODY = 'new body';
14
    public static function getSubscribedEvents(): array
15
    {
16
        return [Events::postPersist];
17
    }
18
19
    public function postPersist(LifecycleEventArgs $event): void
20
    {
21
        if (
22
            !$event->getObject() instanceof Comment
23
            || $event->getObject()->getBody() !== self::COMMENT_BODY
24
        ) {
25
            return;
26
        }
27
28
        $event->getObject()->setBody(self::NEW_COMMENT_BODY);
29
        $event->getEntityManager()->flush();
30
    }
31
}
32