Passed
Push — develop ( ef53f3...80bc53 )
by BENARD
08:34
created

TopicUserLastVisit   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 38
c 1
b 0
f 0
dl 0
loc 110
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isTopicRead() 0 9 2
A getTopic() 0 3 1
A setLastVisitedAt() 0 3 1
A setTopic() 0 3 1
A setIsNotify() 0 3 1
A __construct() 0 3 1
A setUser() 0 3 1
A getIsNotify() 0 3 1
A wasVisitedAfter() 0 3 1
A updateLastVisit() 0 3 1
A getLastVisitedAt() 0 3 1
A getUser() 0 3 1
A __toString() 0 3 1
A getId() 0 3 1
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\TopicUserLastVisitRepository;
9
use Symfony\Component\Serializer\Annotation\Groups;
10
11
#[ORM\Table(name:'pnf_topic_user_last_visit')]
12
#[ORM\Entity(repositoryClass: TopicUserLastVisitRepository::class)]
13
#[ORM\UniqueConstraint(name: "uniq_topic_user_visit", columns: ["user_id", "topic_id"])]
14
#[ORM\Index(name: "idx_user_last_visited", columns: ["user_id", "last_visited_at"])]
15
#[ORM\Index(name: "idx_topic_last_visited", columns: ["topic_id", "last_visited_at"])]
16
class TopicUserLastVisit
17
{
18
    #[Groups(['topic-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: Topic::class)]
27
    #[ORM\JoinColumn(name:'topic_id', referencedColumnName:'id', nullable:false, onDelete:'CASCADE')]
28
    private Topic $topic;
29
30
    #[Groups(['topic-user-visit:read'])]
31
    #[ORM\Column(type: 'datetime', nullable: false)]
32
    private \DateTime $lastVisitedAt;
33
34
    #[Groups(['topic-user-visit:read'])]
35
    #[ORM\Column(nullable: false, options: ['default' => false])]
36
    private bool $isNotify = false;
37
38
    public function __construct()
39
    {
40
        $this->lastVisitedAt = new \DateTime();
41
    }
42
43
    public function __toString(): string
44
    {
45
        return sprintf('%s - %s (%s)', $this->getUser(), $this->getTopic(), $this->getLastVisitedAt()->format('Y-m-d H:i:s'));
46
    }
47
48
    public function getId(): int
49
    {
50
        return $this->id;
51
    }
52
53
    public function setUser($user): void
54
    {
55
        $this->user = $user;
56
    }
57
58
    public function getUser()
59
    {
60
        return $this->user;
61
    }
62
63
    public function setTopic(Topic $topic): void
64
    {
65
        $this->topic = $topic;
66
    }
67
68
    public function getTopic(): Topic
69
    {
70
        return $this->topic;
71
    }
72
73
    public function setLastVisitedAt(\DateTime $lastVisitedAt): void
74
    {
75
        $this->lastVisitedAt = $lastVisitedAt;
76
    }
77
78
    public function getLastVisitedAt(): \DateTime
79
    {
80
        return $this->lastVisitedAt;
81
    }
82
83
    public function setIsNotify(bool $isNotify): void
84
    {
85
        $this->isNotify = $isNotify;
86
    }
87
88
    public function getIsNotify(): bool
89
    {
90
        return $this->isNotify;
91
    }
92
93
    /**
94
     * Met à jour la date de dernière visite à maintenant
95
     */
96
    public function updateLastVisit(): void
97
    {
98
        $this->lastVisitedAt = new \DateTime();
99
    }
100
101
    /**
102
     * Vérifie si le topic a été visité après une certaine date
103
     */
104
    public function wasVisitedAfter(\DateTime $date): bool
105
    {
106
        return $this->lastVisitedAt > $date;
107
    }
108
109
    /**
110
     * Vérifie si le topic est considéré comme lu par rapport au dernier message
111
     */
112
    public function isTopicRead(): bool
113
    {
114
        $lastMessage = $this->topic->getLastMessage();
115
116
        if (!$lastMessage) {
117
            return true; // Pas de message = considéré comme lu
118
        }
119
120
        return $this->lastVisitedAt >= $lastMessage->getCreatedAt();
121
    }
122
}
123