Passed
Push — develop ( c95ce3...d0c957 )
by BENARD
22:42 queued 19:38
created

Forum::getStatusChoices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ForumBundle\Entity;
6
7
use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
8
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
9
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Get;
14
use ApiPlatform\Metadata\GetCollection;
15
use ApiPlatform\OpenApi\Model;
16
use ApiPlatform\Serializer\Filter\GroupFilter;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Doctrine\Common\Collections\Collection;
19
use Doctrine\ORM\Mapping as ORM;
20
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
21
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
22
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
23
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
24
use ProjetNormandie\ForumBundle\Controller\GetHomeForums;
25
use ProjetNormandie\ForumBundle\Controller\ReadForum;
26
use ProjetNormandie\ForumBundle\Repository\ForumRepository;
27
use ProjetNormandie\ForumBundle\ValueObject\ForumStatus;
28
use Symfony\Component\Serializer\Annotation\Groups;
29
use Symfony\Component\Validator\Constraints as Assert;
30
31
#[ORM\Table(name:'pnf_forum')]
32
#[ORM\Entity(repositoryClass: ForumRepository::class)]
33
#[ORM\EntityListeners(["ProjetNormandie\ForumBundle\EventListener\Entity\ForumListener"])]
34
#[ORM\Index(name: "idx_position", columns: ["position"])]
35
#[ORM\Index(name: "idx_lib_forum", columns: ["lib_forum"])]
36
#[ApiResource(
37
    shortName: 'ForumForum',
38
    operations: [
39
        new GetCollection(
40
            uriTemplate: '/forum_forums',
41
        ),
42
        new Get(
43
            uriTemplate: '/forum_forums/{id}',
44
            security: 'object.getStatus() == "public" or is_granted(object.getRole())',
45
        ),
46
        new Get(
47
            uriTemplate: '/forum_forums/{id}/read',
48
            controller: ReadForum::class,
49
            security: 'is_granted("ROLE_USER")',
50
            openapi: new Model\Operation(
51
                summary: 'Mark forum as read',
52
                description: 'Mark forum as read'
53
            ),
54
        ),
55
    ],
56
    normalizationContext: ['groups' => ['forum:read']
57
    ]
58
)]
59
#[ApiFilter(
60
    SearchFilter::class,
61
    properties: [
62
        'parent' => 'exact',
63
    ]
64
)]
65
#[ApiFilter(
66
    OrderFilter::class,
67
    properties: [
68
        'lastMessage.id"' => 'DESC'
69
    ]
70
)]
71
#[ApiFilter(
72
    GroupFilter::class,
73
    arguments: [
74
        'parameterName' => 'groups',
75
        'overrideDefaultGroups' => true,
76
        'whitelist' => [
77
            'forum:read',
78
            'forum:last-message',
79
            'message:read',
80
            'forum:forum-user-1',
81
            'forum-user:read'
82
        ]
83
    ]
84
)]
85
#[ApiFilter(DateFilter::class, properties: ['lastMessage.createdAt' => DateFilterInterface::EXCLUDE_NULL])]
86
class Forum implements TimestampableInterface, SluggableInterface
87
{
88
    use TimestampableTrait;
89
    use SluggableTrait;
90
91
    #[Groups(['forum:read'])]
92
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
93
    private int $id;
94
95
    #[Groups(['forum:read'])]
96
    #[Assert\Length(max: 255)]
97
    #[ORM\Column(length: 255, nullable: false)]
98
    private string $libForum;
99
100
    #[Groups(['forum:read'])]
101
    #[Assert\Length(max: 255)]
102
    #[ORM\Column(length: 255, nullable: true)]
103
    private string $libForumFr;
104
105
    #[ORM\Column(nullable: true, options: ['default' => 0])]
106
    private int $position = 0;
107
108
    #[Groups(['forum:read'])]
109
    #[ORM\Column(length: 20, nullable: false)]
110
    private string $status = ForumStatus::PUBLIC;
111
112
    #[Groups(['forum:read'])]
113
    #[ORM\Column(length: 50, nullable: true)]
114
    private ?string $role = null;
115
116
    #[Groups(['forum:read'])]
117
    #[ORM\Column(nullable: true, options: ['default' => 0])]
118
    private int $nbMessage = 0;
119
120
    #[Groups(['forum:read'])]
121
    #[ORM\Column(nullable: true, options: ['default' => 0])]
122
    private int $nbTopic = 0;
123
124
    #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'forums')]
125
    #[ORM\JoinColumn(name:'category_id', referencedColumnName:'id', nullable:true)]
126
    private ?Category $category;
127
128
    #[ORM\OneToMany(targetEntity: Topic::class, mappedBy: 'forum')]
129
    private Collection $topics;
130
131
    #[ORM\OneToMany(targetEntity: Forum::class, mappedBy: 'parent')]
132
    private Collection $childrens;
133
134
    #[Groups(['forum:read'])]
135
    #[ORM\ManyToOne(targetEntity: Forum::class, inversedBy: 'childrens')]
136
    #[ORM\JoinColumn(name:'parent_id', referencedColumnName:'id', nullable:true)]
137
    private ?Forum $parent;
138
139
    #[Groups(['forum:read'])]
140
    #[ORM\Column(nullable: false, options: ['default' => false])]
141
    private bool $isParent = false;
142
143
    #[Groups(['forum:last-message'])]
144
    #[ORM\ManyToOne(targetEntity: Message::class, cascade: ['persist'])]
145
    #[ORM\JoinColumn(name:'max_message_id', referencedColumnName:'id', nullable:true, onDelete: 'SET NULL')]
146
    private ?Message $lastMessage;
147
148
    #[Groups(['forum:forum-user'])]
149
    #[ORM\OneToMany(targetEntity: ForumUser::class, mappedBy: 'forum')]
150
    private Collection $forumUser;
151
152
    public function __construct()
153
    {
154
        $this->topics = new ArrayCollection();
155
        $this->forumUser = new ArrayCollection();
156
        $this->childrens = new ArrayCollection();
157
    }
158
159
    public function __toString()
160
    {
161
        return sprintf('%s [%s]', $this->getLibForum(), $this->getId());
162
    }
163
164
    public function setId(int $id): void
165
    {
166
        $this->id = $id;
167
    }
168
169
    public function getId(): ?int
170
    {
171
        return $this->id;
172
    }
173
174
    public function setLibForum(string $libForum): void
175
    {
176
        $this->libForum = $libForum;
177
    }
178
179
    public function getLibForum(): string
180
    {
181
        return $this->libForum;
182
    }
183
184
    public function setLibForumFr(string $libForumFr): void
185
    {
186
        $this->libForumFr = $libForumFr;
187
    }
188
189
    public function getLibForumFr(): string
190
    {
191
        return $this->libForumFr;
192
    }
193
194
    public function setPosition(int $position): void
195
    {
196
        $this->position = $position;
197
    }
198
199
    public function getPosition(): int
200
    {
201
        return $this->position;
202
    }
203
204
    public function setStatus(string $status): void
205
    {
206
        $this->status = $status;
207
    }
208
209
    public function getStatus(): string
210
    {
211
        return $this->status;
212
    }
213
214
    public function setRole(string $role): void
215
    {
216
        $this->role = $role;
217
    }
218
219
    public function getRole(): ?string
220
    {
221
        return $this->role;
222
    }
223
224
    public function setNbMessage(int $nbMessage): void
225
    {
226
        $this->nbMessage = $nbMessage;
227
    }
228
229
    public function getNbMessage(): int
230
    {
231
        return $this->nbMessage;
232
    }
233
234
    public function setNbTopic(int $nbTopic): void
235
    {
236
        $this->nbTopic = $nbTopic;
237
    }
238
239
240
    public function getNbTopic(): int
241
    {
242
        return $this->nbTopic;
243
    }
244
245
    public function setCategory(Category $category = null): void
246
    {
247
        $this->category = $category;
248
    }
249
250
    public function getCategory(): ?Category
251
    {
252
        return $this->category;
253
    }
254
255
    public function setParent(Forum $forum = null): void
256
    {
257
        $this->parent = $forum;
258
    }
259
260
    public function getParent(): ?Forum
261
    {
262
        return $this->parent;
263
    }
264
265
    public function getTopics(): Collection
266
    {
267
        return $this->topics;
268
    }
269
270
    public function getChildrens(): Collection
271
    {
272
        return $this->childrens;
273
    }
274
275
    public function setLastMessage(Message $message = null): void
276
    {
277
        $this->lastMessage = $message;
278
    }
279
280
    public function getLastMessage(): ?Message
281
    {
282
        return $this->lastMessage;
283
    }
284
285
    public function getForumUser(): Collection
286
    {
287
        return $this->forumUser;
288
    }
289
290
    public function setIsParent(bool $isParent): void
291
    {
292
        $this->isParent = $isParent;
293
    }
294
295
    public function getIsParent(): bool
296
    {
297
        return $this->isParent;
298
    }
299
300
    #[Groups(['forum:forum-user-1'])]
301
    public function getForumUser1()
302
    {
303
        return $this->forumUser[0];
304
    }
305
306
    public function getSluggableFields(): array
307
    {
308
        return ['libForum'];
309
    }
310
}
311