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

PostEventSubscriber::prePersist()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Fixtures\Event;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Doctrine\ORM\Events;
8
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
9
10
class PostEventSubscriber implements EventSubscriber
11
{
12
    public const TITLE_VALUE = 'title value from event subscriber';
13
    public const NEW_TITLE_VALUE = 'new title value from event subscriber';
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 Post
24
            || self::TITLE_VALUE !== $event->getObject()->getTitle()
25
        ) {
26
            return;
27
        }
28
29
        $event->getObject()->setTitle(self::NEW_TITLE_VALUE);
30
    }
31
}
32