Passed
Push — master ( ac1c77...8de307 )
by Ghazi
02:05
created

DocumentableTrait::getPresentations()   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
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * BigBlueButton open source conferencing system - https://www.bigbluebutton.org/.
5
 *
6
 * Copyright (c) 2016-2024 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 <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace BigBlueButton\Parameters;
22
23
use BigBlueButton\Parameters\Config\DocumentOptionsStore;
24
25
trait DocumentableTrait
26
{
27
    /**
28
     * @var array<string, mixed>
29
     */
30
    protected array $presentations = [];
31
32
    /**
33
     * @return array<string, mixed>
34
     */
35
    public function getPresentations(): array
36
    {
37
        return $this->presentations;
38
    }
39
40
    public function addPresentation(string $nameOrUrl, $content = null, ?string $filename = null, DocumentOptionsStore $attributes = null): self
41
    {
42
        $this->presentations[$nameOrUrl] = [
43
            'content' => !$content ?: base64_encode($content),
44
            'filename' => $filename,
45
            'attributes' => $attributes
46
        ];
47
48
        return $this;
49
    }
50
51
    public function getPresentationsAsXML(): string
52
    {
53
        $result = '';
54
        if (!empty($this->presentations)) {
55
            $xml    = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><modules/>');
56
            $module = $xml->addChild('module');
57
            $module->addAttribute('name', 'presentation');
58
59
            foreach ($this->presentations as $nameOrUrl => $data) {
60
                $presentation = $module->addChild('document');
61
                if (0 === mb_strpos($nameOrUrl, 'http')) {
62
                    $presentation->addAttribute('url', $nameOrUrl);
63
                } else {
64
                    $presentation->addAttribute('name', $nameOrUrl);
65
                }
66
67
                if (!empty($data['filename'])) {
68
                    $presentation->addAttribute('filename', $data['filename']);
69
                }
70
71
                if (!empty($data['content'])) {
72
                    $presentation[0] = $data['content'];
73
                }
74
75
                // Add attributes using DocumentAttributes class
76
                foreach ($data['attributes']->getAttributes() as $attrName => $attrValue) {
77
                    $presentation->addAttribute($attrName, $attrValue);
78
                }
79
            }
80
            $result = $xml->asXML();
81
        }
82
83
        if (!is_string($result)) {
84
            throw new \RuntimeException('String expected, but ' . gettype($result) . ' received.');
85
        }
86
87
        return $result;
88
    }
89
}
90