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

WebinarSchema::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
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
9
class WebinarSchema
10
{
11
    use JsonDeserializableTrait;
12
13
    const TYPE_WEBINAR = 5;
14
    const TYPE_RECURRING_NO_FIXED_TIME = 6;
15
    const TYPE_RECURRING_FIXED_TIME = 9;
16
17
    public $uuid;
18
    public $id;
19
    public $host_id;
20
    public $host_email;
21
    public $topic;
22
    public $type;
23
    public $start_time;
24
    public $duration;
25
    public $timezone;
26
    public $agenda;
27
    public $created_at;
28
    public $start_url;
29
    public $join_url;
30
    public $registration_url;
31
    public $password;
32
    /**
33
     * @var WebinarSettings
34
     */
35
    public $settings;
36
    public $registrants_confirmation_email;
37
    /**
38
     * @var array<int, TrackingField>
39
     */
40
    public $tracking_fields;
41
    public $recurrence;
42
    public $template_id;
43
    /**
44
     * @var array<int, Ocurrence>
45
     */
46
    public $ocurrences;
47
48
    protected function __construct()
49
    {
50
        $this->tracking_fields = [];
51
        $this->settings = new WebinarSettings();
52
        $this->ocurrences = [];
53
    }
54
55
    public function itemClass($propertyName): string
56
    {
57
        if ('tracking_fields' === $propertyName) {
58
            return TrackingField::class;
59
        }
60
61
        if ('ocurrences' === $propertyName) {
62
            return Ocurrence::class;
63
        }
64
65
        throw new Exception("no such array property $propertyName");
66
    }
67
68
    public static function fromTopicAndType($topic, $type = self::TYPE_WEBINAR): WebinarSchema
69
    {
70
        $instance = new static();
71
        $instance->topic = $topic;
72
        $instance->type = $type;
73
74
        return $instance;
75
    }
76
77
    /**
78
     * @throws Exception
79
     */
80
    public function create($userId = null): WebinarSchema
81
    {
82
        $client = Client::getInstance();
83
84
        $userId = empty($userId) ? 'me' : $userId;
85
86
        return self::fromJson(
87
            $client->send('POST', "users/$userId/webinars", [], $this)
88
        );
89
    }
90
91
    /**
92
     * @throws Exception
93
     */
94
    public function update()
95
    {
96
        Client::getInstance()->send('PATCH', 'webinars/'.$this->id, [], $this);
97
    }
98
99
    /**
100
     * @throws Exception
101
     */
102
    public function delete()
103
    {
104
        Client::getInstance()->send('DELETE', "webinars/$this->id");
105
    }
106
107
    public function requiresDateAndDuration(): bool
108
    {
109
        return self::TYPE_WEBINAR == $this->type;
110
    }
111
112
    public function requireRegistration(): bool
113
    {
114
        return in_array(
115
            $this->settings->approval_type,
116
            [
117
                WebinarSettings::APPROVAL_TYPE_AUTOMATICALLY_APPROVE,
118
                WebinarSettings::APPROVAL_TYPE_MANUALLY_APPROVE,
119
            ]
120
        );
121
    }
122
}
123