Skip to content

Commit 30d3705

Browse files
committed
Add TrackedEntity class
Signed-off-by: Tim Goudriaan <tim@codedmonkey.com>
1 parent e44be6f commit 30d3705

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

phpstan.dist.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ parameters:
1818
identifier: method.notFound
1919
count: 1
2020
path: src/DependencyInjection/DirigentConfiguration.php
21+
-
22+
message: '#^Class CodedMonkey\\Dirigent\\Doctrine\\Entity\\TrackedEntity has an uninitialized readonly property \$createdAt\. Assign it in the constructor\.$#'
23+
identifier: property.uninitializedReadonly
24+
count: 1
25+
path: src/Doctrine/Entity/TrackedEntity.php
26+
-
27+
message: '#^Readonly property CodedMonkey\\Dirigent\\Doctrine\\Entity\\TrackedEntity\:\:\$createdAt is assigned outside of the constructor\.$#'
28+
identifier: property.readOnlyAssignNotInConstructor
29+
count: 1
30+
path: src/Doctrine/Entity/TrackedEntity.php
2131
-
2232
message: '#^Left side of \|\| is always false\.$#'
2333
identifier: booleanOr.leftAlwaysFalse
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Doctrine\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
#[ORM\MappedSuperclass]
8+
#[ORM\HasLifecycleCallbacks]
9+
abstract class TrackedEntity
10+
{
11+
#[ORM\Column]
12+
protected readonly \DateTimeImmutable $createdAt;
13+
14+
#[ORM\Column(nullable: true)]
15+
protected ?\DateTimeImmutable $lastModifiedAt = null;
16+
17+
public function getCreatedAt(): \DateTimeImmutable
18+
{
19+
return $this->createdAt;
20+
}
21+
22+
#[ORM\PrePersist]
23+
public function setCreatedAt(): void
24+
{
25+
$this->createdAt ??= new \DateTimeImmutable();
26+
}
27+
28+
public function getLastModifiedAt(): ?\DateTimeImmutable
29+
{
30+
return $this->lastModifiedAt;
31+
}
32+
33+
#[ORM\PreUpdate]
34+
public function setLastModifiedAt(): void
35+
{
36+
$this->lastModifiedAt = new \DateTimeImmutable();
37+
}
38+
}

0 commit comments

Comments
 (0)