AframeDOMProcessor   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 282
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
createDocumentType() 0 1 ?
createDocument() 0 1 ?
A appendFormatComment() 0 7 2
A correctOutputFormat() 0 18 1
A renderHead() 0 8 1
A appendDefaultMetaTags() 0 9 2
A getDefaultMetaTags() 0 16 1
A appendCDN() 0 7 2
A renderBody() 0 4 1
A appendMetaTag() 0 7 2
A createDocType() 0 4 1
A createAframeDocument() 0 6 1
A documentBootstrap() 0 11 2
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 29, 2016 - 8:06:17 AM
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         AframeDOMProcessor.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
trait AframeDOMProcessor
27
{
28
    /**
29
     * A-Frame DOM Document type
30
     *
31
     * @var \DOMDocumentType
32
     */
33
    protected $doctypeObj;
34
35
    /**
36
     * A-Frame DOM Document
37
     *
38
     * @var \DOMDocument
39
     */
40
    protected $docObj;
41
42
    /**
43
     * Scene meta tile
44
     *
45
     * @var string $scene_title
46
     */
47
    protected $scene_title = 'Untitled';
48
49
    /**
50
     * Scene meta description
51
     *
52
     * @var string $scene_description
53
     */
54
    protected $scene_description = '';
55
56
    /**
57
     * <head>
58
     *
59
     * @var \DOMElement
60
     */
61
    protected $head;
62
63
    /**
64
     * <body>
65
     *
66
     * @var \DOMElement
67
     */
68
    protected $body;
69
70
    /**
71
     * <a-scene>
72
     *
73
     * @var \DOMElement
74
     */
75
    protected $scene;
76
77
    /**
78
     * <a-assets>
79
     *
80
     * @var \DOMElement
81
     */
82
    protected $assets;
83
84
    /************
85
     * CONFIG
86
     ***********/
87
88
    /**
89
     * Nicely formats output with indentation and extra space.
90
     *
91
     * @var bool
92
     */
93
    protected $format_output = false;
94
95
    /**
96
     * CDN Of aframe.js
97
     *
98
     * @var string
99
     */
100
    protected $cdn_url;
101
102
    /**
103
     * Whether to use CDN
104
     *
105
     * @var bool $use_cdn
106
     */
107
    protected $use_cdn = false;
108
109
    /**
110
     * aframe assets URI relative to App's base URL / domain
111
     *
112
     * @var string assets_uri
113
     */
114
    protected $assets_uri;
115
116
    /**
117
     * \DOMImplementation::createDocumentType
118
     *
119
     * DOMImplementation::createDocumentType — Creates an empty DOMDocumentType object
120
     *
121
     * {@inheritdoc}
122
     *
123
     * @param string $qualifiedName
124
     * @param null|string $publicId
125
     * @param null|string $systemId
126
     * @return \DOMDocumentType
127
     */
128
    abstract protected function createDocumentType($qualifiedName, $publicId, $systemId);
129
130
    /**
131
     * \DOMImplementation::createDocument
132
     *
133
     * {@inheritdoc}
134
     *
135
     * @param string $namespaceURI
136
     * @param string $qualifiedName
137
     * @param \DOMDocumentType $doctype
138
     * @return \DOMDocument
139
     */
140
    abstract protected function createDocument($namespaceURI, $qualifiedName, \DOMDocumentType $doctype);
141
142
    /**
143
     * Add document comment for formatting
144
     *
145
     * @param string $element
146
     * @param string $comment
147
     */
148 17
    protected function appendFormatComment(string $element, string $comment)
149
    {
150 17
        if ($this->format_output) {
151 4
            $com = $this->docObj->createComment($comment);
152 4
            $this->{$element}->appendChild($com);
153
        }
154 17
    }
155
156
    /**
157
     * Correct html format for tags which are not supported by DOMDocument
158
     *
159
     * @param string $html
160
     * @return string
161
     */
162 4
    protected function correctOutputFormat($html)
163
    {
164
        $tags   = array(
165 4
            '<!--',
166
            '-->',
167
            '<a-assets>',
168
            '</a-assets>',
169
            '</a-scene>'
170
        );
171
        $values = array(
172 4
            '',
173
            "\t",
174
            "\n\t<a-assets>",
175
            "\n\t</a-assets>",
176
            "\n</a-scene>"
177
        );
178 4
        return str_ireplace($tags, $values, $html);
179
    }
180
181
    /**
182
     * Prepeare head
183
     *
184
     * @return void
185
     */
186 13
    protected function renderHead()
187
    {
188 13
        $title = $this->docObj->createElement('title', $this->scene_title);
189 13
        $this->head->appendChild($title);
190 13
        $this->appendDefaultMetaTags();
191 13
        $this->appendCDN();
192 13
        $this->appendScripts($this->scripts);
0 ignored issues
show
Bug introduced by
The property scripts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like appendScripts() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
193 13
    }
194
195
    /**
196
     * Append deffault metatags
197
     *
198
     * @return void
199
     */
200 13
    protected function appendDefaultMetaTags()
201
    {
202 13
        $this->appendMetaTag(array(
203 13
            'name' => 'description',
204 13
            'content' => $this->scene_description
205
        ));
206 13
        foreach ($this->getDefaultMetaTags() as $tag)
207 13
            $this->appendMetaTag($tag);
208 13
    }
209
210
    /**
211
     * Get default meta tags
212
     *
213
     * @return array
214
     */
215 13
    protected function getDefaultMetaTags(): array
216
    {
217 13
        $dmt               = array();
218 13
        $dmt[0]['charset'] = 'utf-8';
219 13
        $dmt[1]['name']    = 'viewport';
220
221 13
        $vp = 'width=device-width,initial-scale=1,maximum-scale=1,shrink-to-fit=no,user-scalable=no,minimal-ui';
222
223 13
        $dmt[1]['content'] = $vp;
224 13
        $dmt[2]['name']    = 'mobile-web-app-capable';
225 13
        $dmt[2]['content'] = 'yes';
226 13
        $dmt[3]['name']    = 'theme-color';
227 13
        $dmt[3]['content'] = 'black';
228
229 13
        return $dmt;
230
    }
231
232
    /**
233
     * If requested by user use aframe CDN
234
     *
235
     * @return void
236
     */
237 13
    protected function appendCDN()
238
    {
239 13
        $cdn_url    = !empty($this->use_cdn) ? $this->cdn_url : sprintf('%s/aframe.min.js',$this->assets_uri);
240 13
        $cdn_script = $this->docObj->createElement('script');
241 13
        $cdn_script->setAttribute('src', $cdn_url);
242 13
        $this->head->appendChild($cdn_script);
243 13
    }
244
245
    /**
246
     * Prepare body
247
     *
248
     * @return void
249
     */
250 13
    protected function renderBody()
251
    {
252 13
        $this->body->appendChild($this->scene);
253 13
    }
254
255
    /**
256
     * Create meta tags
257
     *
258
     * @param array $attr
259
     */
260 13
    protected function appendMetaTag(array $attr)
261
    {
262 13
        $metatag = $this->docObj->createElement('meta');
263 13
        foreach ($attr as $key => $val)
264 13
            $metatag->setAttribute($key, $val);
265 13
        $this->head->appendChild($metatag);
266 13
    }
267
268
    /**
269
     * Creates an empty DOMDocumentType object
270
     *
271
     * @param string $doctype
272
     * @return void
273
     */
274 103
    protected function createDocType(string $doctype)
275
    {
276 103
        $this->doctypeObj = $this->createDocumentType($doctype, null, null);
277 103
    }
278
279
    /**
280
     * Creates a DOMDocument object of the specified type with its document element
281
     *
282
     * @return void
283
     */
284 103
    protected function createAframeDocument()
285
    {
286 103
        $this->docObj = $this->createDocument(null, 'html', $this->doctypeObj);
287
288 103
        $this->docObj->formatOutput = $this->format_output;
289 103
    }
290
291
    /**
292
     * Create dom elements for DOMDocument
293
     *
294
     * @return void
295
     */
296 103
    protected function documentBootstrap()
297
    {
298
        /* Create <head> element */
299 103
        $this->head = $this->docObj->createElement('head');
300
        /* Create <body> element */
301 103
        $this->body = $this->docObj->createElement('body', $this->format_output ? "\n" : '');
302
        /* Create <a-scene> element */
303 103
        $this->scene = $this->docObj->createElement('a-scene');
304
        /* Create <a-assets> element */
305 103
        $this->assets = $this->docObj->createElement('a-assets');
306 103
    }
307
}
308