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

WebinarSchema   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 60
c 1
b 0
f 0
dl 0
loc 131
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 3 1
A fromTopicAndType() 0 7 1
A itemClass() 0 11 3
A update() 0 3 1
A create() 0 8 2
A __construct() 0 5 1
A addRegistrant() 0 8 2
A removeRegistrants() 0 15 3
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 JsonDeserializableTrait;
13
14
    const TYPE_WEBINAR = 5;
15
    const TYPE_RECURRING_NO_FIXED_TIME = 6;
16
    const TYPE_RECURRING_FIXED_TIME = 9;
17
18
    public $uuid;
19
    public $id;
20
    public $host_id;
21
    public $host_email;
22
    public $topic;
23
    public $type;
24
    public $start_time;
25
    public $duration;
26
    public $timezone;
27
    public $agenda;
28
    public $created_at;
29
    public $start_url;
30
    public $join_url;
31
    public $registration_url;
32
    public $password;
33
    /**
34
     * @var WebinarSettings
35
     */
36
    public $settings;
37
    public $registrants_confirmation_email;
38
    /**
39
     * @var array<int, TrackingField>
40
     */
41
    public $tracking_fields;
42
    public $recurrence;
43
    public $template_id;
44
    /**
45
     * @var array<int, Ocurrence>
46
     */
47
    public $ocurrences;
48
49
    protected function __construct()
50
    {
51
        $this->tracking_fields = [];
52
        $this->settings = new WebinarSettings();
53
        $this->ocurrences = [];
54
    }
55
56
    public function itemClass($propertyName): string
57
    {
58
        if ('tracking_fields' === $propertyName) {
59
            return TrackingField::class;
60
        }
61
62
        if ('ocurrences' === $propertyName) {
63
            return Ocurrence::class;
64
        }
65
66
        throw new Exception("no such array property $propertyName");
67
    }
68
69
    public static function fromTopicAndType($topic, $type = self::TYPE_WEBINAR): WebinarSchema
70
    {
71
        $instance = new static();
72
        $instance->topic = $topic;
73
        $instance->type = $type;
74
75
        return $instance;
76
    }
77
78
    /**
79
     * @throws Exception
80
     */
81
    public function create($userId = null): WebinarSchema
82
    {
83
        $client = Client::getInstance();
84
85
        $userId = empty($userId) ? 'me' : $userId;
86
87
        return self::fromJson(
88
            $client->send('POST', "users/$userId/webinars", [], $this)
89
        );
90
    }
91
92
    /**
93
     * @throws Exception
94
     */
95
    public function update()
96
    {
97
        Client::getInstance()->send('PATCH', 'webinars/'.$this->id, [], $this);
98
    }
99
100
    /**
101
     * @throws Exception
102
     */
103
    public function delete()
104
    {
105
        Client::getInstance()->send('DELETE', "webinars/$this->id");
106
    }
107
108
    /**
109
     * @throws Exception
110
     */
111
    public function addRegistrant(RegistrantSchema $registrant, string $ocurrenceIds = ''): CreatedRegistration
112
    {
113
        return CreatedRegistration::fromJson(
114
            Client::getInstance()->send(
115
                'POST',
116
                "webinars/$this->id/registrants",
117
                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...
118
                $registrant
119
            )
120
        );
121
    }
122
123
    /**
124
     * @throws Exception
125
     */
126
    public function removeRegistrants(array $registrants, string $occurrenceIds = '')
127
    {
128
        if (empty($registrants)) {
129
            return;
130
        }
131
132
        $requestBody = new stdClass();
133
        $requestBody->action = 'cancel';
134
        $requestBody->registrants = $registrants;
135
136
        Client::getInstance()->send(
137
            'PUT',
138
            "webinars/$this->id/registrants/status",
139
            empty($occurrenceIds) ? [] : ['occurrence_ids' => $occurrenceIds],
140
            $requestBody
141
        );
142
    }
143
}
144