Passed
Pull Request — master (#5143)
by Angel Fernando Quiroz
07:06
created

ResourceLink::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Metadata\ApiResource;
10
use Chamilo\CoreBundle\Repository\ResourceLinkRepository;
11
use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
12
use Chamilo\CourseBundle\Entity\CGroup;
13
use DateTimeInterface;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\ORM\Mapping as ORM;
17
use Gedmo\Mapping\Annotation as Gedmo;
18
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
19
use LogicException;
20
use Stringable;
21
use Symfony\Component\Serializer\Annotation\Groups;
22
23
#[ApiResource]
24
#[ORM\Table(name: 'resource_link')]
25
#[ORM\Entity(repositoryClass: ResourceLinkRepository::class)]
26
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
27
class ResourceLink implements Stringable
28
{
29
    use TimestampableTypedEntity;
30
    use SoftDeleteableEntity;
31
32
    public const VISIBILITY_DRAFT = 0;
33
    public const VISIBILITY_PENDING = 1;
34
    public const VISIBILITY_PUBLISHED = 2;
35
36
    #[ORM\Id]
37
    #[ORM\Column(type: 'integer')]
38
    #[ORM\GeneratedValue]
39
    protected ?int $id = null;
40
41
    #[ORM\ManyToOne(targetEntity: ResourceNode::class, inversedBy: 'resourceLinks')]
42
    #[ORM\JoinColumn(name: 'resource_node_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
43
    protected ResourceNode $resourceNode;
44
45
    #[Gedmo\SortableGroup]
46
    #[ORM\ManyToOne(targetEntity: Course::class)]
47
    #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
48
    protected ?Course $course = null;
49
50
    #[Gedmo\SortableGroup]
51
    #[ORM\ManyToOne(targetEntity: Session::class, inversedBy: 'resourceLinks')]
52
    #[ORM\JoinColumn(name: 'session_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
53
    protected ?Session $session = null;
54
55
    #[Gedmo\SortableGroup]
56
    #[ORM\ManyToOne(targetEntity: Usergroup::class)]
57
    #[ORM\JoinColumn(name: 'usergroup_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
58
    protected ?Usergroup $userGroup = null;
59
60
    #[Gedmo\SortableGroup]
61
    #[ORM\ManyToOne(targetEntity: CGroup::class)]
62
    #[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'iid', nullable: true, onDelete: 'CASCADE')]
63
    protected ?CGroup $group = null;
64
65
    #[Gedmo\SortableGroup]
66
    #[ORM\ManyToOne(targetEntity: User::class)]
67
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
68
    protected ?User $user = null;
69
70
    /**
71
     * @var Collection<int, ResourceRight>
72
     */
73
    #[ORM\OneToMany(
74
        mappedBy: 'resourceLink',
75
        targetEntity: ResourceRight::class,
76
        cascade: ['persist', 'remove'],
77
        orphanRemoval: true
78
    )]
79
    protected Collection $resourceRights;
80
81
    #[Groups([
82
        'ctool:read',
83
        'c_tool_intro:read',
84
        'student_publication:read',
85
    ])]
86
    #[ORM\Column(name: 'visibility', type: 'integer', nullable: false)]
87
    protected int $visibility;
88
89
    #[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])]
90
    #[ORM\Column(name: 'start_visibility_at', type: 'datetime', nullable: true)]
91
    protected ?DateTimeInterface $startVisibilityAt = null;
92
93
    #[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])]
94
    #[ORM\Column(name: 'end_visibility_at', type: 'datetime', nullable: true)]
95
    protected ?DateTimeInterface $endVisibilityAt = null;
96
97
    #[Gedmo\SortablePosition]
98
    #[ORM\Column]
99
    private int $displayOrder;
100
101
    #[Gedmo\SortableGroup]
102
    #[ORM\Column]
103
    private ?int $resourceTypeGroup;
104
105
    public function __construct()
106
    {
107
        $this->resourceRights = new ArrayCollection();
108
        $this->visibility = self::VISIBILITY_DRAFT;
109
    }
110
111
    public function __toString(): string
112
    {
113
        return (string) $this->getId();
114
    }
115
116
    public function getId(): ?int
117
    {
118
        return $this->id;
119
    }
120
121
    public function getStartVisibilityAt(): ?DateTimeInterface
122
    {
123
        return $this->startVisibilityAt;
124
    }
125
126
    public function setStartVisibilityAt(?DateTimeInterface $startVisibilityAt): self
127
    {
128
        $this->startVisibilityAt = $startVisibilityAt;
129
130
        return $this;
131
    }
132
133
    public function getEndVisibilityAt(): ?DateTimeInterface
134
    {
135
        return $this->endVisibilityAt;
136
    }
137
138
    public function setEndVisibilityAt(?DateTimeInterface $endVisibilityAt): self
139
    {
140
        $this->endVisibilityAt = $endVisibilityAt;
141
142
        return $this;
143
    }
144
145
    public function addResourceRight(ResourceRight $right): self
146
    {
147
        if (!$this->resourceRights->contains($right)) {
148
            $right->setResourceLink($this);
149
            $this->resourceRights->add($right);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return Collection<int, ResourceRight>
157
     */
158
    public function getResourceRights(): Collection
159
    {
160
        return $this->resourceRights;
161
    }
162
163
    public function setResourceRights(Collection $rights): self
164
    {
165
        $this->resourceRights = $rights;
166
167
        /*foreach ($rights as $right) {
168
              $this->addResourceRight($right);
169
          }*/
170
171
        return $this;
172
    }
173
174
    public function getCourse(): ?Course
175
    {
176
        return $this->course;
177
    }
178
179
    public function setCourse(?Course $course = null): self
180
    {
181
        $this->course = $course;
182
183
        return $this;
184
    }
185
186
    public function getSession(): ?Session
187
    {
188
        return $this->session;
189
    }
190
191
    public function setSession(?Session $session = null): self
192
    {
193
        $this->session = $session;
194
195
        return $this;
196
    }
197
198
    public function hasSession(): bool
199
    {
200
        return null !== $this->session;
201
    }
202
203
    public function hasCourse(): bool
204
    {
205
        return null !== $this->course;
206
    }
207
208
    public function hasGroup(): bool
209
    {
210
        return null !== $this->group;
211
    }
212
213
    public function getGroup(): ?CGroup
214
    {
215
        return $this->group;
216
    }
217
218
    public function setGroup(?CGroup $group = null): self
219
    {
220
        $this->group = $group;
221
222
        return $this;
223
    }
224
225
    public function getUserGroup(): ?Usergroup
226
    {
227
        return $this->userGroup;
228
    }
229
230
    public function setUserGroup(?Usergroup $group = null): self
231
    {
232
        $this->userGroup = $group;
233
234
        return $this;
235
    }
236
237
    public function getUser(): ?User
238
    {
239
        return $this->user;
240
    }
241
242
    public function setUser(?User $user = null): self
243
    {
244
        $this->user = $user;
245
246
        return $this;
247
    }
248
249
    public function hasUser(): bool
250
    {
251
        return null !== $this->user;
252
    }
253
254
    public function getResourceNode(): ResourceNode
255
    {
256
        return $this->resourceNode;
257
    }
258
259
    public function setResourceNode(ResourceNode $resourceNode): self
260
    {
261
        $this->resourceNode = $resourceNode;
262
263
        return $this;
264
    }
265
266
    public function isPublished(): bool
267
    {
268
        return self::VISIBILITY_PUBLISHED === $this->getVisibility();
269
    }
270
271
    public function getVisibility(): int
272
    {
273
        return $this->visibility;
274
    }
275
276
    public function setVisibility(int $visibility): self
277
    {
278
        if (!\in_array($visibility, self::getVisibilityList(), true)) {
279
            $message = sprintf(
280
                'The visibility is not valid. Valid options: %s',
281
                print_r(self::getVisibilityList(), true)
282
            );
283
284
            throw new LogicException($message);
285
        }
286
        $this->visibility = $visibility;
287
288
        return $this;
289
    }
290
291
    public static function getVisibilityList(): array
292
    {
293
        return [
294
            'Draft' => self::VISIBILITY_DRAFT,
295
            'Pending' => self::VISIBILITY_PENDING,
296
            'Published' => self::VISIBILITY_PUBLISHED,
297
        ];
298
    }
299
300
    public function isPending(): bool
301
    {
302
        return self::VISIBILITY_PENDING === $this->getVisibility();
303
    }
304
305
    public function isDraft(): bool
306
    {
307
        return self::VISIBILITY_DRAFT === $this->getVisibility();
308
    }
309
310
    public function getVisibilityName(): string
311
    {
312
        return array_flip(static::getVisibilityList())[$this->getVisibility()];
313
    }
314
315
    public function getDisplayOrder(): int
316
    {
317
        return $this->displayOrder;
318
    }
319
320
    public function setDisplayOrder(int $displayOrder): static
321
    {
322
        $this->displayOrder = $displayOrder;
323
324
        return $this;
325
    }
326
327
    public function getResourceTypeGroup(): ?int
328
    {
329
        return $this->resourceTypeGroup;
330
    }
331
332
    public function setResourceTypeGroup(int $resourceTypeGroup): static
333
    {
334
        $this->resourceTypeGroup = $resourceTypeGroup;
335
336
        return $this;
337
    }
338
}
339