1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProjetNormandie\ForumBundle\Entity; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use ProjetNormandie\ForumBundle\Repository\ForumUserLastVisitRepository; |
9
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
10
|
|
|
|
11
|
|
|
#[ORM\Table(name:'pnf_forum_user_last_visit')] |
12
|
|
|
#[ORM\Entity(repositoryClass: ForumUserLastVisitRepository::class)] |
13
|
|
|
#[ORM\UniqueConstraint(name: "uniq_forum_user_visit", columns: ["user_id", "forum_id"])] |
14
|
|
|
#[ORM\Index(name: "idx_user_last_visited", columns: ["user_id", "last_visited_at"])] |
15
|
|
|
#[ORM\Index(name: "idx_forum_last_visited", columns: ["forum_id", "last_visited_at"])] |
16
|
|
|
class ForumUserLastVisit |
17
|
|
|
{ |
18
|
|
|
#[Groups(['forum-user-visit:read'])] |
19
|
|
|
#[ORM\Id, ORM\Column, ORM\GeneratedValue] |
20
|
|
|
private int $id; |
21
|
|
|
|
22
|
|
|
#[ORM\ManyToOne(targetEntity: UserInterface::class)] |
23
|
|
|
#[ORM\JoinColumn(name:'user_id', referencedColumnName:'id', nullable:false, onDelete:'CASCADE')] |
24
|
|
|
private $user; |
25
|
|
|
|
26
|
|
|
#[ORM\ManyToOne(targetEntity: Forum::class)] |
27
|
|
|
#[ORM\JoinColumn(name:'forum_id', referencedColumnName:'id', nullable:false, onDelete:'CASCADE')] |
28
|
|
|
private Forum $forum; |
29
|
|
|
|
30
|
|
|
#[Groups(['forum-user-visit:read'])] |
31
|
|
|
#[ORM\Column(type: 'datetime', nullable: false)] |
32
|
|
|
private \DateTime $lastVisitedAt; |
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->lastVisitedAt = new \DateTime(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __toString(): string |
40
|
|
|
{ |
41
|
|
|
return sprintf( |
42
|
|
|
'%s - %s (%s)', |
43
|
|
|
$this->getUser(), |
44
|
|
|
$this->getForum(), |
45
|
|
|
$this->getLastVisitedAt()->format('Y-m-d H:i:s') |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getId(): int |
50
|
|
|
{ |
51
|
|
|
return $this->id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setUser($user): void |
55
|
|
|
{ |
56
|
|
|
$this->user = $user; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getUser() |
60
|
|
|
{ |
61
|
|
|
return $this->user; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setForum(Forum $forum): void |
65
|
|
|
{ |
66
|
|
|
$this->forum = $forum; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getForum(): Forum |
70
|
|
|
{ |
71
|
|
|
return $this->forum; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setLastVisitedAt(\DateTime $lastVisitedAt): void |
75
|
|
|
{ |
76
|
|
|
$this->lastVisitedAt = $lastVisitedAt; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getLastVisitedAt(): \DateTime |
80
|
|
|
{ |
81
|
|
|
return $this->lastVisitedAt; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Met à jour la date de dernière visite à maintenant |
86
|
|
|
*/ |
87
|
|
|
public function updateLastVisit(): void |
88
|
|
|
{ |
89
|
|
|
$this->lastVisitedAt = new \DateTime(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Vérifie si le forum a été visité après une certaine date |
94
|
|
|
*/ |
95
|
|
|
public function wasVisitedAfter(\DateTime $date): bool |
96
|
|
|
{ |
97
|
|
|
return $this->lastVisitedAt > $date; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|