Skip to content

Commit 26f9056

Browse files
committed
Added basic entity listener to track changes to payment order fields
1 parent 13437b9 commit 26f9056

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/Entity/FieldChanges.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private function __construct(private array $changedFields, private string|null $
3030
* @param \DateTimeImmutable|null $dateTime
3131
* @return void
3232
*/
33-
public function changeField(string $fieldName, string $user, ?\DateTimeImmutable $dateTime): void
33+
public function changeField(string $fieldName, string $user, ?\DateTimeImmutable $dateTime = null): void
3434
{
3535
$this->changedFields[$fieldName] = [
3636
'date' => $dateTime ?? new \DateTimeImmutable(),

src/Entity/PaymentOrder.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use App\Entity\Contracts\TimestampedElementInterface;
2323
use App\Entity\Embeddable\Confirmation;
2424
use App\Entity\Embeddable\PayeeInfo;
25+
use App\EntityListener\PaymentOrderChangedFieldsListener;
2526
use App\Repository\PaymentOrderRepository;
2627
use App\Validator\FSRNotBlocked;
2728
use DateTime;
@@ -47,6 +48,7 @@
4748
#[ORM\HasLifecycleCallbacks]
4849
#[ORM\Table('payment_orders')]
4950
#[Vich\Uploadable]
51+
#[ORM\EntityListeners([PaymentOrderChangedFieldsListener::class])]
5052
class PaymentOrder implements DBElementInterface, TimestampedElementInterface, \Serializable
5153
{
5254
use TimestampTrait;
@@ -275,8 +277,8 @@ class PaymentOrder implements DBElementInterface, TimestampedElementInterface, \
275277
/**
276278
* @var FieldChanges|null The changes that were made to this payment order (or null if old case, where no changes were tracked)
277279
*/
278-
#[ORM\Column(type: 'field_changes', nullable: true)]
279-
private ?FieldChanges $fieldChanges = null;
280+
#[ORM\Column(type: 'field_changes', nullable: true, name: 'field_changes')]
281+
private ?FieldChanges $field_changes = null;
280282

281283
/**
282284
* @var Collection The confirmation tokens that can be used to confirm this payment order
@@ -847,11 +849,11 @@ public function removeConfirmationToken(ConfirmationToken $confirmationToken): s
847849
public function getFieldChanges(): FieldChanges
848850
{
849851
//If no field changes are set, create a new one
850-
if ($this->fieldChanges === null) {
851-
$this->fieldChanges = FieldChanges::new();
852+
if ($this->field_changes === null) {
853+
$this->field_changes = FieldChanges::new();
852854
}
853855

854-
return $this->fieldChanges;
856+
return $this->field_changes;
855857
}
856858

857859
public function serialize(): ?string
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace App\EntityListener;
7+
8+
use App\Entity\PaymentOrder;
9+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
10+
use Doctrine\ORM\Event\PreUpdateEventArgs;
11+
12+
#[AsEntityListener]
13+
class PaymentOrderChangedFieldsListener
14+
{
15+
/**
16+
* The fields whose changes we want to track
17+
*/
18+
private const FIELDS_WHITELIST = [
19+
'submitter_name',
20+
'submitter_email',
21+
'amount',
22+
'project_name',
23+
//TODO
24+
];
25+
26+
public function preUpdate(PreUpdateEventArgs $eventArgs): void
27+
{
28+
$entity = $eventArgs->getObject();
29+
30+
// only act on PaymentOrder entities
31+
if (!$entity instanceof PaymentOrder) {
32+
return;
33+
}
34+
35+
// get the changed fields
36+
$changedFields = array_keys($eventArgs->getEntityChangeSet());
37+
38+
// filter out the fields we are interested in
39+
$fieldsToBeSaved = array_intersect($changedFields, self::FIELDS_WHITELIST);
40+
41+
// if no fields are to be saved, we can return early
42+
if (empty($fieldsToBeSaved)) {
43+
return;
44+
}
45+
46+
//TODO: Determine Username
47+
$user = 'StuRa Finanzen';
48+
49+
//Otherwise add the change to the field_changes property for each changed field
50+
$fieldChanges = $entity->getFieldChanges();
51+
foreach ($fieldsToBeSaved as $field) {
52+
$fieldChanges->changeField($field, $user);
53+
}
54+
55+
//As changes are not tracked anymore, we write out the changes to the database changeset ourselves
56+
$eventArgs->setNewValue('field_changes', $fieldChanges);
57+
}
58+
}

0 commit comments

Comments
 (0)