UserBan   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 45
c 1
b 0
f 0
dl 0
loc 118
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A isActive() 0 3 1
A setUser() 0 3 1
A getReason() 0 3 1
A __toString() 0 8 2
A getId() 0 3 1
A getUser() 0 3 1
A isPermanent() 0 3 1
A setReason() 0 3 1
A setActive() 0 3 1
A setStartDate() 0 3 1
A __construct() 0 3 1
A setEndDate() 0 3 1
A getStartDate() 0 3 1
A getEndDate() 0 3 1
A isCurrentlyBanned() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\UserBundle\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Gedmo\Timestampable\Traits\TimestampableEntity;
9
use Symfony\Component\Validator\Constraints as Assert;
10
11
#[ORM\Table(name: 'pnu_user_ban')]
12
#[ORM\Entity]
13
#[ORM\Index(columns: ["user_id", "start_date", "end_date"], name: "ban_search_idx")]
14
class UserBan
15
{
16
    use TimestampableEntity;
17
18
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
19
    private ?int $id = null;
20
21
22
    #[Assert\NotNull]
23
    #[ORM\ManyToOne(targetEntity: User::class)]
24
    #[ORM\JoinColumn(name: "user_id", referencedColumnName: "id", onDelete: "CASCADE")]
25
    private User $user;
26
27
    #[Assert\NotNull]
28
    #[ORM\Column(type: 'date')]
29
    private \DateTime $startDate;
30
31
32
    #[ORM\Column(type: 'date', nullable: true)]
33
    private ?\DateTime $endDate = null;
34
35
    #[ORM\Column(type: 'text', nullable: true)]
36
    private ?string $reason = null;
37
38
    #[ORM\Column(type: 'boolean', options: ['default' => true])]
39
    private bool $active = true;
40
41
    public function __construct()
42
    {
43
        $this->startDate = new \DateTime();
44
    }
45
46
    public function getId(): ?int
47
    {
48
        return $this->id;
49
    }
50
51
    public function getUser(): User
52
    {
53
        return $this->user;
54
    }
55
56
    public function setUser(User $user): void
57
    {
58
        $this->user = $user;
59
    }
60
61
    public function getStartDate(): \DateTime
62
    {
63
        return $this->startDate;
64
    }
65
66
    public function setStartDate(\DateTime $startDate): void
67
    {
68
        $this->startDate = $startDate;
69
    }
70
71
    public function getEndDate(): ?\DateTime
72
    {
73
        return $this->endDate;
74
    }
75
76
    public function setEndDate(?\DateTime $endDate): void
77
    {
78
        $this->endDate = $endDate;
79
    }
80
81
    public function getReason(): ?string
82
    {
83
        return $this->reason;
84
    }
85
86
    public function setReason(?string $reason): void
87
    {
88
        $this->reason = $reason;
89
    }
90
91
    public function isActive(): bool
92
    {
93
        return $this->active;
94
    }
95
96
    public function setActive(bool $active): void
97
    {
98
        $this->active = $active;
99
    }
100
101
    public function isCurrentlyBanned(): bool
102
    {
103
        if (!$this->active) {
104
            return false;
105
        }
106
107
        $now = new \DateTime();
108
109
        if ($this->endDate === null) {
110
            return $now >= $this->startDate;
111
        }
112
113
        return $now >= $this->startDate && $now <= $this->endDate;
114
    }
115
116
    public function isPermanent(): bool
117
    {
118
        return $this->endDate === null;
119
    }
120
121
    public function __toString(): string
122
    {
123
        return sprintf(
124
            'Ban #%d for %s (%s to %s)',
125
            $this->id,
126
            $this->user->getUsername(),
127
            $this->startDate->format('Y-m-d H:i'),
128
            $this->endDate ? $this->endDate->format('Y-m-d H:i') : 'permanent'
129
        );
130
    }
131
}
132