Passed
Branch feature/frontend (0b874a)
by Stone
04:52
created

AbstractAppSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 50
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setEm() 0 3 1
A sendToDatabase() 0 5 1
A flush() 0 3 1
A persist() 0 4 1
A deleteFromDatabase() 0 5 1
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use App\Event\AbstractAppEvent;
6
use App\FlashMessage\AddFlashTrait;
7
use Doctrine\ORM\EntityManagerInterface;
8
9
abstract class AbstractAppSubscriber
10
{
11
    use AddFlashTrait;
12
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    protected $em;
17
18
    /**
19
     * @return array The event names to listen to
20
     */
21
    abstract public static function getSubscribedEvents();
22
23
    /**
24
     * @param AbstractAppEvent $event
25
     * Registers the trick into the database
26
     */
27
    public function sendToDatabase(AbstractAppEvent $event)
28
    {
29
        $entity = $event->getEntity();
30
        $this->em->persist($entity);
31
        $this->em->flush();
32
    }
33
34
    public function deleteFromDatabase(AbstractAppEvent $event)
35
    {
36
        $entity = $event->getEntity();
37
        $this->em->remove($entity);
38
        $this->em->flush();
39
    }
40
41
    public function persist(AbstractAppEvent $event)
42
    {
43
        $entity = $event->getEntity();
44
        $this->em->persist($entity);
45
    }
46
47
    public function flush()
48
    {
49
        $this->em->flush();
50
    }
51
52
    /**
53
     * @required
54
     * @param EntityManagerInterface $em
55
     */
56
    public function setEm(EntityManagerInterface $em): void
57
    {
58
        $this->em = $em;
59
    }
60
}