Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
| User | |
100.00% |
26 / 26 |
|
100.00% |
15 / 15 |
18 | |
100.00% |
1 / 1 |
| getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getEmail | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setEmail | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getCreatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCreatedAtString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| setCreatedAt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setCreatedAtNow | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUpdatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUpdatedAtString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| setUpdatedAt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setUpdatedAtNow | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isValidEmail | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Entity; |
| 6 | |
| 7 | use App\Exception\Entity\User\InvalidEmailException; |
| 8 | use App\Repository\UserRepository; |
| 9 | use Doctrine\ORM\Mapping as ORM; |
| 10 | |
| 11 | #[ORM\Entity(repositoryClass: UserRepository::class)] |
| 12 | #[ORM\HasLifecycleCallbacks] |
| 13 | #[ORM\Table(name: '`users`')] |
| 14 | class User |
| 15 | { |
| 16 | protected const CANONICAL_FORMAT = 'Y-m-d H:i:s'; |
| 17 | |
| 18 | #[ORM\Id] |
| 19 | #[ORM\GeneratedValue] |
| 20 | #[ORM\Column] |
| 21 | private ?int $id = null; |
| 22 | |
| 23 | #[ORM\Column(length: 150)] |
| 24 | private ?string $name = null; |
| 25 | |
| 26 | #[ORM\Column(length: 150)] |
| 27 | private ?string $email = null; |
| 28 | |
| 29 | #[ORM\Column] |
| 30 | private ?\DateTimeImmutable $createdAt = null; |
| 31 | |
| 32 | #[ORM\Column(nullable: true)] |
| 33 | private ?\DateTimeImmutable $updatedAt = null; |
| 34 | |
| 35 | public function getId(): ?int |
| 36 | { |
| 37 | return $this->id; |
| 38 | } |
| 39 | |
| 40 | public function setId(int $id): static |
| 41 | { |
| 42 | $this->id = $id; |
| 43 | |
| 44 | return $this; |
| 45 | } |
| 46 | |
| 47 | public function getName(): ?string |
| 48 | { |
| 49 | return $this->name; |
| 50 | } |
| 51 | |
| 52 | public function setName(string $name): static |
| 53 | { |
| 54 | $this->name = $name; |
| 55 | |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | public function getEmail(): ?string |
| 60 | { |
| 61 | return $this->email; |
| 62 | } |
| 63 | |
| 64 | public function setEmail(string $email): static |
| 65 | { |
| 66 | if (!self::isValidEmail($email)) { |
| 67 | throw InvalidEmailException::create($email); |
| 68 | } |
| 69 | $this->email = $email; |
| 70 | |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | public function getCreatedAt(): ?\DateTimeImmutable |
| 75 | { |
| 76 | return $this->createdAt; |
| 77 | } |
| 78 | |
| 79 | public function getCreatedAtString(): ?string |
| 80 | { |
| 81 | if (!$this->createdAt instanceof \DateTimeImmutable) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | return $this->createdAt->format(self::CANONICAL_FORMAT); |
| 86 | } |
| 87 | |
| 88 | public function setCreatedAt(\DateTimeImmutable $createdAt): static |
| 89 | { |
| 90 | $this->createdAt = $createdAt; |
| 91 | |
| 92 | return $this; |
| 93 | } |
| 94 | |
| 95 | #[ORM\PrePersist] |
| 96 | public function setCreatedAtNow(): self |
| 97 | { |
| 98 | return $this->setCreatedAt(new \DateTimeImmutable('now')); |
| 99 | } |
| 100 | |
| 101 | public function getUpdatedAt(): ?\DateTimeImmutable |
| 102 | { |
| 103 | return $this->updatedAt; |
| 104 | } |
| 105 | |
| 106 | public function getUpdatedAtString(): ?string |
| 107 | { |
| 108 | if (!$this->updatedAt instanceof \DateTimeImmutable) { |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | return $this->updatedAt->format(self::CANONICAL_FORMAT); |
| 113 | } |
| 114 | |
| 115 | public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static |
| 116 | { |
| 117 | $this->updatedAt = $updatedAt; |
| 118 | |
| 119 | return $this; |
| 120 | } |
| 121 | |
| 122 | #[ORM\PreUpdate] |
| 123 | public function setUpdatedAtNow(): self |
| 124 | { |
| 125 | return $this->setUpdatedAt(new \DateTimeImmutable('now')); |
| 126 | } |
| 127 | |
| 128 | protected static function isValidEmail(string $email): bool |
| 129 | { |
| 130 | return \filter_var($email, FILTER_VALIDATE_EMAIL) !== false; |
| 131 | } |
| 132 | } |