Passed
Push — develop ( 5e1cbc...3bd182 )
by BENARD
03:40
created

Forum::getParent()   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
dl 0
loc 3
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 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
    #[Groups(['forum:last-message'])]
134
    #[ORM\ManyToOne(targetEntity: Message::class, cascade: ['persist'])]
135
    #[ORM\JoinColumn(name:'max_message_id', referencedColumnName:'id', nullable:true, onDelete: 'SET NULL')]
136
    private ?Message $lastMessage;
137
138
    #[Groups(['forum:forum-user'])]
139
    #[ORM\OneToMany(targetEntity: ForumUser::class, mappedBy: 'forum')]
140
    private Collection $forumUser;
141
142
    public function __construct()
143
    {
144
        $this->topics = new ArrayCollection();
145
        $this->forumUser = new ArrayCollection();
146
    }
147
148
    public function __toString()
149
    {
150
        return sprintf('%s [%s]', $this->getLibForum(), $this->getId());
151
    }
152
153
    public function setId(int $id): void
154
    {
155
        $this->id = $id;
156
    }
157
158
    public function getId(): ?int
159
    {
160
        return $this->id;
161
    }
162
163
    public function setLibForum(string $libForum): void
164
    {
165
        $this->libForum = $libForum;
166
    }
167
168
    public function getLibForum(): string
169
    {
170
        return $this->libForum;
171
    }
172
173
    public function setLibForumFr(string $libForumFr): void
174
    {
175
        $this->libForumFr = $libForumFr;
176
    }
177
178
    public function getLibForumFr(): string
179
    {
180
        return $this->libForumFr;
181
    }
182
183
    public function setPosition(int $position): void
184
    {
185
        $this->position = $position;
186
    }
187
188
    public function getPosition(): int
189
    {
190
        return $this->position;
191
    }
192
193
    public function setStatus(string $status): void
194
    {
195
        $this->status = $status;
196
    }
197
198
    public function getStatus(): string
199
    {
200
        return $this->status;
201
    }
202
203
    public function setRole(string $role): void
204
    {
205
        $this->role = $role;
206
    }
207
208
    public function getRole(): ?string
209
    {
210
        return $this->role;
211
    }
212
213
    public function setNbMessage(int $nbMessage): void
214
    {
215
        $this->nbMessage = $nbMessage;
216
    }
217
218
    public function getNbMessage(): int
219
    {
220
        return $this->nbMessage;
221
    }
222
223
    public function setNbTopic(int $nbTopic): void
224
    {
225
        $this->nbTopic = $nbTopic;
226
    }
227
228
229
    public function getNbTopic(): int
230
    {
231
        return $this->nbTopic;
232
    }
233
234
    public function getSlug(): string
235
    {
236
        return $this->slug;
237
    }
238
239
    public function setCategory(?Category $category = null): void
240
    {
241
        $this->category = $category;
242
    }
243
244
    public function getCategory(): ?Category
245
    {
246
        return $this->category;
247
    }
248
249
250
    public function getTopics(): Collection
251
    {
252
        return $this->topics;
253
    }
254
255
    public function setLastMessage(?Message $message = null): void
256
    {
257
        $this->lastMessage = $message;
258
    }
259
260
    public function getLastMessage(): ?Message
261
    {
262
        return $this->lastMessage;
263
    }
264
265
    public function getForumUser(): Collection
266
    {
267
        return $this->forumUser;
268
    }
269
270
    #[Groups(['forum:forum-user-1'])]
271
    public function getForumUser1()
272
    {
273
        return $this->forumUser[0];
274
    }
275
}
276