Passed
Pull Request — master (#189)
by Ghazi
10:24
created

DocumentableTrait::getPresentationsAsXML()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 26
rs 9.3888
cc 5
nc 2
nop 0
1
<?php
2
3
/*
4
 * BigBlueButton open source conferencing system - https://www.bigbluebutton.org/.
5
 *
6
 * Copyright (c) 2016-2022 BigBlueButton Inc. and by respective authors (see below).
7
 *
8
 * This program is free software; you can redistribute it and/or modify it under the
9
 * terms of the GNU Lesser General Public License as published by the Free Software
10
 * Foundation; either version 3.0 of the License, or (at your option) any later
11
 * version.
12
 *
13
 * BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
14
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License along
18
 * with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace BigBlueButton\Parameters;
22
23
trait DocumentableTrait
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $presentations = [];
29
30
    /**
31
     * @return array
32
     */
33
    public function getPresentations()
34
    {
35
        return $this->presentations;
36
    }
37
38
    /**
39
     * @param $nameOrUrl
40
     * @param null $content
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $content is correct as it would always require null to be passed?
Loading history...
41
     * @param null $filename
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $filename is correct as it would always require null to be passed?
Loading history...
42
     *
43
     * @return $this
44
     */
45
    public function addPresentation($nameOrUrl, $content = null, $filename = null)
46
    {
47
        if (!$filename) {
0 ignored issues
show
introduced by
$filename is of type null, thus it always evaluated to false.
Loading history...
48
            $this->presentations[$nameOrUrl] = !$content ?: base64_encode($content);
0 ignored issues
show
introduced by
$content is of type null, thus it always evaluated to false.
Loading history...
49
        } else {
50
            $this->presentations[$nameOrUrl] = $filename;
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    public function getPresentationsAsXML()
60
    {
61
        $result = '';
62
63
        if (!empty($this->presentations)) {
64
            $xml    = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><modules/>');
65
            $module = $xml->addChild('module');
66
            $module->addAttribute('name', 'presentation');
67
68
            foreach ($this->presentations as $nameOrUrl => $content) {
69
                if (0 === mb_strpos($nameOrUrl, 'http')) {
70
                    $presentation = $module->addChild('document');
71
                    $presentation->addAttribute('url', $nameOrUrl);
72
                    if (is_string($content)) {
73
                        $presentation->addAttribute('filename', $content);
74
                    }
75
                } else {
76
                    $document = $module->addChild('document');
77
                    $document->addAttribute('name', $nameOrUrl);
78
                    $document[0] = $content;
79
                }
80
            }
81
            $result = $xml->asXML();
82
        }
83
84
        return $result;
85
    }
86
}
87