Test Setup Failed
Push — develop ( dc2cdb...883a72 )
by Stone
04:31
created

AppSubscriber::sendToDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use App\Event\AppEvent;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
8
9
abstract class AppSubscriber
10
{
11
    /**
12
     * @var EntityManagerInterface
13
     */
14
    protected $em;
15
16
    /**
17
     * @var FlashBagInterface
18
     */
19
    protected $flashBag;
20
21
    /**
22
     * @return array The event names to listen to
23
     */
24
    abstract public static function getSubscribedEvents();
25
26
    /**
27
     * @param AppEvent $event
28
     * Registers the trick into the database
29
     */
30
    public function sendToDatabase(AppEvent $event)
31
    {
32
        $entity = $event->getEntity();
33
        $this->em->persist($entity);
34
        $this->em->flush();
35
    }
36
37
    public function deleteFromDatabase(AppEvent $event)
38
    {
39
        $entity = $event->getEntity();
40
        $this->em->remove($entity);
41
        $this->em->flush();
42
    }
43
44
    public function persist(AppEvent $event)
45
    {
46
        $entity = $event->getEntity();
47
        $this->em->persist($entity);
48
    }
49
50
    public function flush()
51
    {
52
        $this->em->flush();
53
    }
54
55
    /**
56
     * Adding te flash message to the session. This enables to have the same syntax as in the controllers
57
     * @param string $type
58
     * @param string $message
59
     */
60
    public function addFlash(string $type, string $message)
61
    {
62
        $this->flashBag->add($type, $message);
63
    }
64
65
    /**
66
     * @required
67
     * @param EntityManagerInterface $em
68
     */
69
    public function setEm(EntityManagerInterface $em): void
70
    {
71
        $this->em = $em;
72
    }
73
74
    /**
75
     * @required
76
     * @param FlashBagInterface $flashBag
77
     */
78
    public function setFlashBag(FlashBagInterface $flashBag): void
79
    {
80
        $this->flashBag = $flashBag;
81
    }
82
}