Completed
Push — master ( a17b83...17edca )
by Marko
02:55
created

AframeDOMDocument   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 246
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A render() 0 17 3
A setTitle() 0 4 1
A setDescription() 0 4 1
A appendEntities() 0 8 3
A appendAssets() 0 13 4
A appendAsset() 0 5 1
A appendEntity() 0 5 1
A renderSceneOnly() 0 9 2
B configOptions() 0 7 5
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 27, 2016 - 9:55:09 PM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         AframeDOMDocument.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 ^ @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Core\DOM;
25
26
use \AframeVR\Core\Config;
27
use \DOMImplementation;
28
use \DOMDocumentType;
29
use \DOMDocument;
30
use \AframeVR\Core\Entity;
31
use \AframeVR\Interfaces\AssetsInterface;
32
33
final class AframeDOMDocument extends DOMImplementation
34
{
35
    use AframeDOMProcessor;
36
37
    /**
38
     * A-Frame DOM Document type
39
     *
40
     * @var \DOMDocumentType
41
     */
42
    protected $doctypeObj;
43
44
    /**
45
     * A-Frame DOM Document
46
     *
47
     * @var \DOMDocument
48
     */
49
    protected $docObj;
50
51
    /**
52
     * Scene meta tile
53
     *
54
     * @var string $scene_title
55
     */
56
    protected $scene_title = 'Untitled';
57
58
    /**
59
     * Scene meta description
60
     *
61
     * @var string $scene_description
62
     */
63
    protected $scene_description = '';
64
65
    /**
66
     * <head>
67
     *
68
     * @var \DOMElement
69
     */
70
    protected $head;
71
72
    /**
73
     * <body>
74
     *
75
     * @var \DOMElement
76
     */
77
    protected $body;
78
79
    /**
80
     * <a-scene>
81
     *
82
     * @var \DOMElement
83
     */
84
    protected $scene;
85
86
    /**
87
     * <a-assets>
88
     *
89
     * @var \DOMElement
90
     */
91
    protected $assets;
92
93
    /************
94
     * CONFIG
95
     ***********/
96
    
97
    /**
98
     * Nicely formats output with indentation and extra space.
99
     *
100
     * @var bool
101
     */
102
    protected $format_output = false;
103
104
    /**
105
     * CDN Of aframe.js
106
     *
107
     * @var string
108
     */
109
    protected $cdn_url;
110
    
111
    /**
112
     * Whether to use CDN
113
     *
114
     * @var bool $use_cdn
115
     */
116
    protected $use_cdn = false;
117
    
118
    /**
119
     * aframe assets URI relative to App's base URL / domain
120
     * 
121
     * @var string assets_uri
122
     */
123
    protected $assets_uri;
124
    
125
    /**
126
     * A-Frame DOM
127
     *
128
     * @param Config $config            
129
     */
130 71
    public function __construct(Config $config)
131
    {
132
        /* Config */
133 71
        $this->configOptions($config);
134
        
135
        /* Create HTML5 Document type */
136 71
        $this->createDocType('html');
137
        
138
        /* Create A-Frame DOM Document */
139 71
        $this->createAframeDocument();
140
        
141
        /* Create boostrap elements */
142 71
        $this->documentBootstrap();
143 71
    }
144
145
    /**
146
     * Render scene this DOM Object is attached to
147
     *
148
     * @return string
149
     */
150 5
    public function render(): string
151
    {
152 5
        $this->docObj->formatOutput = $this->format_output;
153
        
154 5
        $html = $this->docObj->getElementsByTagName('html')->item(0);
155
        /* Make sure we do not add duplicates when render is called multiple times */
156 5
        if (! $html->hasChildNodes()) {
157 5
            $this->renderHead();
158
            
159 5
            $html->appendChild($this->head);
160
            
161 5
            $this->renderBody();
162
            
163 5
            $html->appendChild($this->body);
164
        }
165 5
        return $this->format_output ? $this->correctOutputFormat($this->docObj->saveHTML()) : $this->docObj->saveHTML();
166
    }
167
168
    /**
169
     * Set Scene meta title
170
     *
171
     * @param string $title            
172
     */
173 7
    public function setTitle(string $title)
174
    {
175 7
        $this->scene_title = $title;
176 7
    }
177
178
    /**
179
     * Set Scene meta description
180
     *
181
     * @param string $description            
182
     */
183 7
    public function setDescription(string $description)
184
    {
185 7
        $this->scene_description = $description;
186 7
    }
187
188
    /**
189
     * Append entities
190
     *
191
     * @param array $entities            
192
     * @return void
193
     */
194 7
    public function appendEntities(array $entities)
195
    {
196 7
        if (! empty($entities)) {
197 2
            foreach ($entities as $entity) {
198 2
                $this->appendEntity($entity);
199
            }
200
        }
201 7
    }
202
203
    /**
204
     * Append assets
205
     *
206
     * @param array $assets            
207
     * @return void
208
     */
209 2
    public function appendAssets(array $assets)
210
    {
211 2
        if (! empty($assets)) {
212 2
            if ($this->format_output) {
213 2
                $com = $this->docObj->createComment('');
214 2
                $this->scene->appendChild($com);
215
            }
216 2
            foreach ($assets as $asset) {
217 2
                $this->appendAsset($asset);
218
            }
219 2
            $this->scene->appendChild($this->assets);
220
        }
221 2
    }
222
223
    /**
224
     * Append asset
225
     *
226
     * Create asset DOMElement
227
     *
228
     * @param AssetsInterface $asset            
229
     */
230 2
    public function appendAsset(AssetsInterface $asset)
231
    {
232 2
        $this->appendFormatComment('assets', "\n\t");
233 2
        $this->assets->appendChild($asset->domElement($this->docObj));
234 2
    }
235
236
    /**
237
     * Create entity DOMElement
238
     *
239
     * Created entity and append it to scene
240
     *
241
     * @param Entity $entity            
242
     * @return void
243
     */
244 2
    public function appendEntity(Entity $entity)
245
    {
246 2
        $this->appendFormatComment('scene', "\n");
247 2
        $this->scene->appendChild($entity->domElement($this->docObj));
248 2
    }
249
250
    /**
251
     * Get HTML of Scene only
252
     *
253
     * @return string
254
     */
255 3
    public function renderSceneOnly()
256
    {
257 3
        $html               = new DOMDocument();
258 3
        $html->formatOutput = $this->format_output;
259
        
260 3
        $html_scene = $html->importNode($this->scene, true);
261 3
        $html->appendChild($html_scene);
262 3
        return $this->format_output ? $this->correctOutputFormat($html->saveHTML()) : $html->saveHTML();
263
    }
264
    
265
    /**
266
     * Set configuration option related to DOM
267
     * 
268
     * @param Config $config
269
     * @return void
270
     */
271 71
    protected function configOptions(Config $config)
272
    {
273 71
        $this->format_output = is_bool($config->get('format_output')) ? $config->get('format_output') : false;
274 71
        $this->cdn_url       = is_string($config->get('cdn_url')) ? $config->get('cdn_url') : null;
275 71
        $this->use_cdn       = is_bool($config->get('use_cdn')) ? $config->get('use_cdn') : false;
276 71
        $this->assets_uri    = is_string($config->get('assets_uri')) ? $config->get('assets_uri') : '/aframe';
277 71
    }
278
}
279