Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseActivitySubscriber | |
0.00% |
0 / 32 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| preUpdate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| postUpdate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| prePersist | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
132 | |||
| postPersist | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getServerDateTime | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | // src/EventListener/DatabaseActivitySubscriber.php |
| 6 | |
| 7 | namespace Satag\AmicronEntityBundle\EventListener; |
| 8 | |
| 9 | use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; |
| 10 | use Doctrine\ORM\EntityManagerInterface; |
| 11 | use Doctrine\ORM\Event\PostPersistEventArgs; |
| 12 | use Doctrine\ORM\Event\PostUpdateEventArgs; |
| 13 | use Doctrine\ORM\Event\PrePersistEventArgs; |
| 14 | use Doctrine\ORM\Event\PreUpdateEventArgs; |
| 15 | use Doctrine\ORM\Events; |
| 16 | use Satag\AmicronEntityBundle\Entity\AbstractAufgabeTermin; |
| 17 | use Satag\AmicronEntityBundle\Entity\AdressgruppeAdresse; |
| 18 | use Satag\AmicronEntityBundle\Entity\Benutzer; |
| 19 | use Satag\AmicronEntityBundle\Entity\TextbausteinZuordnung; |
| 20 | use Satag\AmicronEntityBundle\Traits\BlameableEntityInterface; |
| 21 | use Satag\AmicronEntityBundle\Traits\CreatorEntityInterface; |
| 22 | use 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)] |
| 28 | class 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 | } |