Passed
Pull Request — master (#5143)
by Angel Fernando Quiroz
09:05
created

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