Passed
Push — master ( 2661ba...d91ec9 )
by Francis
02:48 queued 35s
created

Event::validate()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 6
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 11
rs 9.2222
1
<?php
2
3
namespace LiveStream\Resources;
4
5
use LiveStream\Resources\Resource;
6
use LiveStream\Interfaces\Updatable\Updatable;
0 ignored issues
show
Bug introduced by
The type LiveStream\Interfaces\Updatable\Updatable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use LiveStream\Exceptions\InValidResourceException;
8
use LiveStream\Interfaces\Resource as ResourceInterface;
9
10
class Event extends Resource implements ResourceInterface, Updatable
11
{
12
    /**
13
     * Class Constructor.
14
     *
15
     * @param string  $fullName
16
     * @param boolean $init
17
     */
18
    public function __construct(string $fullName, bool $init = true)
19
    {
20
        parent::__construct($init);
21
        if ($init)
22
            $this->data->fullName  = $fullName;
23
    }
24
25
    /**
26
     * Set Event Start Time.
27
     *
28
     * @param  string $strtime
29
     * @return \LiveStream\Resources\Event
30
     */
31
    public function setStartTime(string $strtime): Event
32
    {
33
        $this->data->startTime = date('c', strtotime($strtime));
34
        return $this;
35
    }
36
37
    /**
38
     * Set End Time
39
     *
40
     * @param  string $strtime
41
     * @return \LiveStream\Resources\Event
42
     */
43
    public function setEndTime(string $strtime): Event
44
    {
45
        $this->data->endTime = date('c', strtotime($strtime));
46
        return $this;
47
    }
48
49
    /**
50
     * Set Is Draft.
51
     *
52
     * @param boolean $isDraft
53
     * 
54
     * @return \LiveStream\Resources\Event
55
     */
56
    public function setIsDraft(bool $isDraft = true): Event
57
    {
58
        $this->data->draft = $isDraft;
59
        return $this;
60
    }
61
62
    /**
63
     * Get Is Draft.
64
     *
65
     * @return boolean
66
     */
67
    public function isDraft(): bool
68
    {
69
        return $this->data->draft ?? true;
70
    }
71
72
    /**
73
     * Add Event Tag
74
     *
75
     * @param  string $tag
76
     * @return \LiveStream\Resources\Event
77
     */
78
    public function addTag(string $tag): Event
79
    {
80
        if (!isset($this->data->tags)) $this->data->tags = '';
81
82
        $this->data->tags .= rtrim($tag, ',') . ',';
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get Tags.
89
     *
90
     * @return string
91
     */
92
    public function getTags()
93
    {
94
        return $this->data->tags ?? '';
95
    }
96
97
    /**
98
     * Undocumented function
99
     *
100
     * @param string $name
101
     * @param array $arguments
102
     * @return void
103
     */
104
    public function __call(string $name, array $arguments)
105
    {
106
        switch ($name) {
107
            case 'getDraft':
108
                return $this->isDraft();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->isDraft() returns the type boolean which is incompatible with the documented return type void.
Loading history...
109
            case 'setDraft':
110
                return $this->setIsDraft($arguments[0]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setIsDraft($arguments[0]) returns the type LiveStream\Resources\Event which is incompatible with the documented return type void.
Loading history...
111
            default:
112
                return parent::__call($name, $arguments);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::__call($name, $arguments) targeting LiveStream\Resources\Resource::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
        }
114
    }
115
116
    /**
117
     * Resource Interface Method: Get Resource as JSON String.
118
     *
119
     * @return string
120
     */
121
    public function getResourceBody(): string
122
    {
123
        $body = ['fullName' => $this->data->fullName];
124
125
        $this->set_fields($body, [
126
            'shortName'                 => $this->data->shortName ?? null,
127
            'startTime'                 => $this->data->startTime ?? null,
128
            'endTime'                   => $this->data->endTime ?? null,
129
            'draft'                     => $this->data->draft ?? null,
130
            'description'               => $this->data->description ?? null,
131
            'tags'                      => ($this->data->tags ?? null) ? rtrim($this->data->tags, ',') : null,
132
            'isPublic'                  => $this->data->isPublic ?? null,
133
            'isSearchable'              => $this->data->isSearchable ?? null,
134
            'viewerCountVisible'        => $this->data->viewerCountVisible ?? null,
135
            'postCommentsEnabled'       => $this->data->postCommentsEnabled ?? null,
136
            'liveChatEnabled'           => $this->data->liveChatEnabled ?? null,
137
            'isEmbeddable'              => $this->data->isEmbeddable ?? null,
138
            'isPasswordProtected'       => $this->data->isPasswordProtected ?? null,
139
            'password'                  => $this->data->password ?? null,
140
            'isWhiteLabeled'            => $this->data->isWhiteLabeled ?? null,
141
            'embedRestriction'          => $this->data->embedRestriction ?? null,
142
            'embedRestrictionWhitelist' => $this->data->embedRestrictionWhitelist ?? null,
143
            'embedRestrictionBlacklist' => $this->data->embedRestrictionBlacklist ?? null,
144
        ]);
145
146
        return json_encode($body);
147
    }
148
149
    /**
150
     * Undocumented function
151
     *
152
     * @param  array $array
153
     * @param  array $map
154
     * @return void
155
     */
156
    private function set_fields(array &$array, array $map): void
157
    {
158
        foreach ($map as $key => $value) {
159
            if ($value)
160
                $array[$key] = $value;
161
        }
162
    }
163
164
    /**
165
     * Undocumented function
166
     *
167
     * @return string
168
     */
169
    public function getContentType(): string
170
    {
171
        return 'application/json';
172
    }
173
174
    /**
175
     * Validates the Event Resource.
176
     * 
177
     * @param bool $exists Whether resource is exists (being updated) or not.
178
     * 
179
     * @return void
180
     */
181
    public function validate(bool $exists = false): void
182
    {
183
        if (!$this->data->fullName ?? null) throw new InValidResourceException('Event', 'fullName');
184
185
        if (($this->data->isPasswordProtected ?? false) && (!$this->data->password ?? null)) {
186
            throw new InValidResourceException('Event', 'password (password must be present for a password protected event)');
187
        }
188
189
        if ($exists) {                   
190
            if (!$this->data->id ?? null) {
191
                throw new InValidResourceException('Event', 'id (Event ID must be present while updating an event.)');
192
            }
193
        }
194
    }
195
196
    /**
197
     * Update Resource.
198
     *
199
     * @return boolean
200
     */
201
    public function update(bool $new = true): bool
202
    {
203
        return false;
204
    }
205
}
206