Passed
Pull Request — develop (#29)
by BENARD
14:17 queued 11:49
created

Forum::getSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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