Passed
Pull Request — 1.11.x (#4160)
by Angel Fernando Quiroz
08:51
created

WebinarSchema::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\Zoom\API;
6
7
use Exception;
8
use stdClass;
9
10
class WebinarSchema
11
{
12
    use BaseMeetingTrait;
13
    use JsonDeserializableTrait;
14
15
    const TYPE_WEBINAR = 5;
16
    const TYPE_RECURRING_NO_FIXED_TIME = 6;
17
    const TYPE_RECURRING_FIXED_TIME = 9;
18
19
    public $uuid;
20
    public $id;
21
    public $host_id;
22
    public $created_at;
23
    public $start_url;
24
    public $join_url;
25
    public $registration_url;
26
    public $password;
27
    /**
28
     * @var WebinarSettings
29
     */
30
    public $settings;
31
    public $registrants_confirmation_email;
32
    /**
33
     * @var array<int, TrackingField>
34
     */
35
    public $tracking_fields;
36
    public $recurrence;
37
    public $template_id;
38
    /**
39
     * @var array<int, Ocurrence>
40
     */
41
    public $ocurrences;
42
43
    protected function __construct()
44
    {
45
        $this->tracking_fields = [];
46
        $this->settings = new WebinarSettings();
47
        $this->ocurrences = [];
48
    }
49
50
    public function itemClass($propertyName): string
51
    {
52
        if ('tracking_fields' === $propertyName) {
53
            return TrackingField::class;
54
        }
55
56
        if ('ocurrences' === $propertyName) {
57
            return Ocurrence::class;
58
        }
59
60
        throw new Exception("no such array property $propertyName");
61
    }
62
63
    public static function fromTopicAndType($topic, $type = self::TYPE_WEBINAR): WebinarSchema
64
    {
65
        $instance = new static();
66
        $instance->topic = $topic;
67
        $instance->type = $type;
68
69
        return $instance;
70
    }
71
72
    /**
73
     * @throws Exception
74
     */
75
    public function create($userId = null): WebinarSchema
76
    {
77
        $client = Client::getInstance();
78
79
        $userId = empty($userId) ? 'me' : $userId;
80
81
        return self::fromJson(
82
            $client->send('POST', "users/$userId/webinars", [], $this)
83
        );
84
    }
85
86
    /**
87
     * @throws Exception
88
     */
89
    public function update(): void
90
    {
91
        Client::getInstance()->send('PATCH', 'webinars/'.$this->id, [], $this);
92
    }
93
94
    /**
95
     * @throws Exception
96
     */
97
    public function delete()
98
    {
99
        Client::getInstance()->send('DELETE', "webinars/$this->id");
100
    }
101
102
    /**
103
     * @throws Exception
104
     */
105
    public function addRegistrant(RegistrantSchema $registrant, string $ocurrenceIds = ''): CreatedRegistration
106
    {
107
        return CreatedRegistration::fromJson(
108
            Client::getInstance()->send(
109
                'POST',
110
                "webinars/$this->id/registrants",
111
                empty($occurrenceIds) ? [] : ['occurrence_ids' => $occurrenceIds],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $occurrenceIds does not exist. Did you maybe mean $ocurrenceIds?
Loading history...
112
                $registrant
113
            )
114
        );
115
    }
116
117
    /**
118
     * @throws Exception
119
     */
120
    public function removeRegistrants(array $registrants, string $occurrenceIds = '')
121
    {
122
        if (empty($registrants)) {
123
            return;
124
        }
125
126
        $requestBody = new stdClass();
127
        $requestBody->action = 'cancel';
128
        $requestBody->registrants = $registrants;
129
130
        Client::getInstance()->send(
131
            'PUT',
132
            "webinars/$this->id/registrants/status",
133
            empty($occurrenceIds) ? [] : ['occurrence_ids' => $occurrenceIds],
134
            $requestBody
135
        );
136
    }
137
}
138