Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
DatabaseActivitySubscriber
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
6 / 6
18
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 preUpdate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 postUpdate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prePersist
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
11
 postPersist
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getServerDateTime
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5// src/EventListener/DatabaseActivitySubscriber.php
6
7namespace Satag\AmicronEntityBundle\EventListener;
8
9use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
10use Doctrine\ORM\EntityManagerInterface;
11use Doctrine\ORM\Event\PostPersistEventArgs;
12use Doctrine\ORM\Event\PostUpdateEventArgs;
13use Doctrine\ORM\Event\PrePersistEventArgs;
14use Doctrine\ORM\Event\PreUpdateEventArgs;
15use Doctrine\ORM\Events;
16use Satag\AmicronEntityBundle\Entity\AbstractAufgabeTermin;
17use Satag\AmicronEntityBundle\Entity\AdressgruppeAdresse;
18use Satag\AmicronEntityBundle\Entity\Benutzer;
19use Satag\AmicronEntityBundle\Entity\TextbausteinZuordnung;
20use Satag\AmicronEntityBundle\Traits\BlameableEntityInterface;
21use Satag\AmicronEntityBundle\Traits\CreatorEntityInterface;
22use Symfony\Bundle\SecurityBundle\Security;
23
24#[AsDoctrineListener(event: Events::prePersist)]
25#[AsDoctrineListener(event: Events::preUpdate)]
26#[AsDoctrineListener(event: Events::postPersist)]
27#[AsDoctrineListener(event: Events::postUpdate)]
28class DatabaseActivitySubscriber
29{
30    public function __construct(private readonly Security $security, private readonly EntityManagerInterface $entityManager)
31    {
32    }
33
34    public function preUpdate(PreUpdateEventArgs $args): void
35    {
36        $entity = $args->getObject();
37        if (is_a($entity, BlameableEntityInterface::class)) {
38            if ($this->security->getUser() instanceof Benutzer) {
39                $kuerzel = $this->security->getUser()->getKuerzel();
40            } else {
41                $kuerzel = 'B0T';
42            }
43
44            $entity->setGeaendertvon($kuerzel);
45            $entity->setGeaendertam($this->getServerDateTime());
46        }
47    }
48
49    public function postUpdate(PostUpdateEventArgs $args): void
50    {
51    }
52
53    public function prePersist(PrePersistEventArgs $args): void
54    {
55        $entity = $args->getObject();
56        if ($this->security->getUser() instanceof Benutzer) {
57            $kuerzel = $this->security->getUser()->getKuerzel();
58        } else {
59            $kuerzel = 'B0T';
60        }
61
62        if (is_a($entity, CreatorEntityInterface::class)) {
63            $entity->setAngelegtvon($kuerzel);
64            $entity->setAngelegtam($this->getServerDateTime());
65        }
66
67        if (is_a($entity, BlameableEntityInterface::class)) {
68            $entity->setGeaendertvon($entity->getAngelegtvon());
69            $entity->setGeaendertam($entity->getAngelegtam());
70        }
71
72        if (is_a($entity, AbstractAufgabeTermin::class)) {
73            if (!$entity->getBenutzer() && $this->security->getUser() instanceof Benutzer) {
74                $entity->setBenutzer($this->security->getUser());
75            }
76        }
77
78        if ($entity instanceof AdressgruppeAdresse) {
79            if (!$entity->getZuordnungsdatum()) {
80                $entity->setZuordnungsdatum($this->getServerDateTime());
81            }
82        }
83        if ($entity instanceof TextbausteinZuordnung) {
84            if (!$entity->getAngelegtam()) {
85                $entity->setAngelegtam($this->getServerDateTime());
86            }
87        }
88    }
89
90    public function postPersist(PostPersistEventArgs $args): void
91    {
92    }
93
94    private function getServerDateTime(): \DateTime
95    {
96        $conn = $this->entityManager->getConnection();
97        $result = $conn->fetchOne('select current_timestamp(0) from rdb$database');
98
99        return new \DateTime($result, new \DateTimeZone('UTC'));
100    }
101}