Passed
Push — master ( 719760...59ab2e )
by Angel Fernando Quiroz
18:35
created

AgendaEventInvitation::removeInviteesNotInIdList()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 16
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Collection;
12
use Doctrine\ORM\Mapping as ORM;
13
14
#[ORM\Table(name: 'agenda_event_invitation')]
15
#[ORM\Entity()]
16
#[ORM\InheritanceType('SINGLE_TABLE')]
17
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
18
#[ORM\DiscriminatorMap([
19
    'invitation' => 'Chamilo\CoreBundle\Entity\AgendaEventInvitation',
20
    'subscription' => 'Chamilo\CoreBundle\Entity\AgendaEventSubscription',
21
])]
22
class AgendaEventInvitation
23
{
24
    use TimestampableTypedEntity;
25
26
    #[ORM\Id]
27
    #[ORM\Column(type: 'bigint')]
28
    #[ORM\GeneratedValue(strategy: 'AUTO')]
29
    protected ?int $id = null;
30
31
    #[ORM\OneToMany(
32
        mappedBy: 'invitation',
33
        targetEntity: AgendaEventInvitee::class,
34
        cascade: ['persist', 'remove'],
35
        orphanRemoval: true
36
    )]
37
    protected Collection $invitees;
38
39
    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'resourceNodes')]
40
    #[ORM\JoinColumn(name: 'creator_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
41
    protected User $creator;
42
43
    public function __construct()
44
    {
45
        $this->invitees = new ArrayCollection();
46
    }
47
48
    public function getId(): ?int
49
    {
50
        return $this->id;
51
    }
52
53
    public function getInvitees(): Collection
54
    {
55
        return $this->invitees;
56
    }
57
58
    public function setInvitees(Collection $invitees): self
59
    {
60
        $this->invitees = $invitees;
61
62
        return $this;
63
    }
64
65
    public function addInvitee(AgendaEventInvitee $invitee): self
66
    {
67
        $invitee->setInvitation($this);
68
        $this->invitees->add($invitee);
69
70
        return $this;
71
    }
72
73
    public function removeInviteeUser(User $user): self
74
    {
75
        /** @var AgendaEventInvitee $invitee */
76
        $invitee = $this
77
            ->invitees
78
            ->filter(function (AgendaEventInvitee $invitee) use ($user) {
79
                return $invitee->getUser() === $user;
80
            })
81
            ->first()
82
        ;
83
84
        if ($invitee) {
0 ignored issues
show
introduced by
$invitee is of type Chamilo\CoreBundle\Entity\AgendaEventInvitee, thus it always evaluated to true.
Loading history...
85
            $this->invitees->removeElement($invitee);
86
            $invitee->setInvitation(null);
87
        }
88
89
        return $this;
90
    }
91
92
    public function removeInvitees(): self
93
    {
94
        $this->invitees = new ArrayCollection();
95
96
        return $this;
97
    }
98
99
    public function getCreator(): User
100
    {
101
        return $this->creator;
102
    }
103
104
    public function setCreator(User $creator): self
105
    {
106
        $this->creator = $creator;
107
108
        return $this;
109
    }
110
111
    public function hasUserAsInvitee(User $user): bool
112
    {
113
        return $this->invitees->exists(
114
            function (int $key, AgendaEventInvitee $invitee) use ($user) {
115
                return $invitee->getUser() === $user;
116
            }
117
        );
118
    }
119
120
    public function removeInviteesNotInIdList(array $idList): self
121
    {
122
        $toRemove = [];
123
124
        /** @var AgendaEventInvitee $invitee */
125
        foreach ($this->invitees as $key => $invitee) {
126
            if (!\in_array($invitee->getUser()->getId(), $idList, true)) {
127
                $toRemove[] = $key;
128
            }
129
        }
130
131
        foreach ($toRemove as $key) {
132
            $this->invitees->remove($key);
133
        }
134
135
        return $this;
136
    }
137
}
138