Passed
Push — master ( 786b21...4a8f5f )
by Stone
06:47 queued 42s
created

AppSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

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

5 Methods

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