Passed
Pull Request — master (#216)
by Charly
03:36
created

CommentEventSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 20
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 3 1
A prePersist() 0 10 3
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Fixtures\Event;
4
5
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Doctrine\ORM\Events;
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
15
    public function getSubscribedEvents(): array
16
    {
17
        return [Events::prePersist];
18
    }
19
20
    public function prePersist(LifecycleEventArgs $event): void
21
    {
22
        if (
23
            !$event->getObject() instanceof Comment
24
            || self::COMMENT_BODY !== $event->getObject()->getBody()
25
        ) {
26
            return;
27
        }
28
29
        $event->getObject()->setBody(self::NEW_COMMENT_BODY);
30
    }
31
}
32