Passed
Push — 1.11.x ( 14e9fc...e0306e )
by Julito
13:47
created

RegistrantEntity::setMeeting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\Zoom;
6
7
use Chamilo\PluginBundle\Zoom\API\CreatedRegistration;
8
use Chamilo\PluginBundle\Zoom\API\MeetingRegistrant;
9
use Chamilo\PluginBundle\Zoom\API\MeetingRegistrantListItem;
10
use Chamilo\UserBundle\Entity\User;
11
use Doctrine\ORM\Mapping as ORM;
12
use Exception;
13
14
/**
15
 * Class RegistrantEntity.
16
 *
17
 * @ORM\Entity(repositoryClass="Chamilo\PluginBundle\Zoom\RegistrantEntityRepository")
18
 * @ORM\Table(
19
 *     name="plugin_zoom_registrant",
20
 *     indexes={
21
 *         @ORM\Index(name="user_id_index", columns={"user_id"}),
22
 *         @ORM\Index(name="meeting_id_index", columns={"meeting_id"}),
23
 *     }
24
 * )
25
 * @ORM\HasLifecycleCallbacks
26
 */
27
class RegistrantEntity
28
{
29
    /** @var string */
30
    public $fullName;
31
32
    /**
33
     * @var string
34
     * @ORM\Column(type="integer")
35
     * @ORM\Id
36
     * @ORM\GeneratedValue(strategy="AUTO")
37
     */
38
    protected $id;
39
40
    /**
41
     * @var User
42
     * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User")
43
     * @ORM\JoinColumn(name="user_id", nullable=false)
44
     */
45
    protected $user;
46
47
    /**
48
     * @var MeetingEntity
49
     * @ORM\ManyToOne(
50
     *     targetEntity="MeetingEntity",
51
     *     inversedBy="registrants",
52
     * )
53
     * @ORM\JoinColumn(name="meeting_id")
54
     */
55
    protected $meeting;
56
57
    /**
58
     * @var string
59
     * @ORM\Column(type="text", name="created_registration_json", nullable=true)
60
     */
61
    protected $createdRegistrationJson;
62
63
    /**
64
     * @var string
65
     * @ORM\Column(type="text", name="meeting_registrant_list_item_json", nullable=true)
66
     */
67
    protected $meetingRegistrantListItemJson;
68
69
    /**
70
     * @var string
71
     * @ORM\Column(type="text", name="meeting_registrant_json", nullable=true)
72
     */
73
    protected $meetingRegistrantJson;
74
75
    /** @var CreatedRegistration */
76
    protected $createdRegistration;
77
78
    /** @var MeetingRegistrant */
79
    protected $meetingRegistrant;
80
81
    /** @var MeetingRegistrantListItem */
82
    protected $meetingRegistrantListItem;
83
84
    /**
85
     * @return string
86
     */
87
    public function __toString()
88
    {
89
        return sprintf('Registrant %d', $this->id);
90
    }
91
92
    /**
93
     * @return MeetingEntity
94
     */
95
    public function getMeeting()
96
    {
97
        return $this->meeting;
98
    }
99
100
    /**
101
     * @param MeetingEntity $meeting
102
     *
103
     * @return $this
104
     */
105
    public function setMeeting($meeting)
106
    {
107
        $this->meeting = $meeting;
108
        $this->meeting->getRegistrants()->add($this);
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return User
115
     */
116
    public function getUser()
117
    {
118
        return $this->user;
119
    }
120
121
    /**
122
     * @param User $user
123
     *
124
     * @return $this
125
     */
126
    public function setUser($user)
127
    {
128
        $this->user = $user;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @return MeetingRegistrantListItem
135
     * @throws Exception
136
     *
137
     */
138
    public function getMeetingRegistrantListItem()
139
    {
140
        return $this->meetingRegistrantListItem;
141
    }
142
143
    /**
144
     * @param MeetingRegistrantListItem $meetingRegistrantListItem
145
     *
146
     * @return $this
147
     * @throws Exception
148
     *
149
     */
150
    public function setMeetingRegistrantListItem($meetingRegistrantListItem)
151
    {
152
        if (!is_null($this->meeting) && $this->meeting->getId() != $meetingRegistrantListItem->id) {
153
            throw new Exception('RegistrantEntity meeting id differs from MeetingRegistrantListItem id');
154
        }
155
        $this->meetingRegistrantListItem = $meetingRegistrantListItem;
156
        $this->computeFullName();
157
158
        return $this;
159
    }
160
161
    public function computeFullName()
162
    {
163
        $this->fullName = api_get_person_name(
164
            $this->meetingRegistrant->first_name,
165
            $this->meetingRegistrant->last_name
166
        );
167
    }
168
169
    public function getJoinUrl()
170
    {
171
        if (!$this->createdRegistration) {
172
            return '';
173
        }
174
175
        return $this->createdRegistration->join_url;
176
    }
177
178
    /**
179
     * @return CreatedRegistration
180
     * @throws Exception
181
     *
182
     */
183
    public function getCreatedRegistration()
184
    {
185
        return $this->createdRegistration;
186
    }
187
188
    /**
189
     * @param CreatedRegistration $createdRegistration
190
     *
191
     * @return $this
192
     * @throws Exception
193
     *
194
     */
195
    public function setCreatedRegistration($createdRegistration)
196
    {
197
        if (null === $this->id) {
198
            $this->id = $createdRegistration->registrant_id;
199
        } elseif ($this->id != $createdRegistration->registrant_id) {
200
            throw new Exception('RegistrantEntity id differs from CreatedRegistration identifier');
201
        }
202
        $this->createdRegistration = $createdRegistration;
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return MeetingRegistrant
209
     * @throws Exception
210
     *
211
     */
212
    public function getMeetingRegistrant()
213
    {
214
        return $this->meetingRegistrant;
215
    }
216
217
    /**
218
     * @param MeetingRegistrant $meetingRegistrant
219
     *
220
     * @return $this
221
     * @throws Exception
222
     *
223
     */
224
    public function setMeetingRegistrant($meetingRegistrant)
225
    {
226
        $this->meetingRegistrant = $meetingRegistrant;
227
        $this->computeFullName();
228
229
        return $this;
230
    }
231
232
    /**
233
     * @ORM\PostLoad
234
     *
235
     * @throws Exception
236
     */
237
    public function postLoad()
238
    {
239
        if (null !== $this->meetingRegistrantJson) {
240
            $this->meetingRegistrant = MeetingRegistrant::fromJson($this->meetingRegistrantJson);
241
        }
242
        if (null !== $this->createdRegistrationJson) {
243
            $this->createdRegistration = CreatedRegistration::fromJson($this->createdRegistrationJson);
244
        }
245
        if (null !== $this->meetingRegistrantListItemJson) {
246
            $this->meetingRegistrantListItem = MeetingRegistrantListItem::fromJson(
247
                $this->meetingRegistrantListItemJson
248
            );
249
        }
250
        $this->computeFullName();
251
    }
252
253
    /**
254
     * @ORM\PreFlush
255
     */
256
    public function preFlush()
257
    {
258
        if (null !== $this->meetingRegistrant) {
259
            $this->meetingRegistrantJson = json_encode($this->meetingRegistrant);
260
        }
261
        if (null !== $this->createdRegistration) {
262
            $this->createdRegistrationJson = json_encode($this->createdRegistration);
263
        }
264
        if (null !== $this->meetingRegistrantListItem) {
265
            $this->meetingRegistrantListItemJson = json_encode($this->meetingRegistrantListItem);
266
        }
267
    }
268
}
269