Completed
Push — master ( 46a40a...5f3088 )
by Marko
03:08
created

Scene::meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 20, 2016 - 9:01:22 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         Scene.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;
25
26
use \AframeVR\Core\Helpers\MetaTags;
27
use \AframeVR\Extras\Primitives;
28
use \DOMImplementation;
29
use \DOMDocument;
30
31
final class Scene
32
{
33
    use Primitives;
34
35
    private $name;
36
37 23
    public function __construct($name)
38
    {
39 23
        $this->name = $name;
40 23
    }
41
42
    /**
43
     * Scene DOMDocument meta tags object
44
     *
45
     * @return \AframeVR\Core\Helpers\MetaTags
46
     */
47 2
    public function meta()
48
    {
49 2
        return $this->meta ?? $this->meta = new MetaTags();
0 ignored issues
show
Bug introduced by
The property meta 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...
50
    }
51
52
    /**
53
     * Render the A-Frame scene
54
     *
55
     * @param bool $full            
56
     * @param bool $print            
57
     */
58 2
    public function render($full = true, $print = true)
59
    {
60 2
        $dom = new DOMImplementation();
61 2
        $doctype = $dom->createDocumentType('html');
62 2
        $aframe_dom = $dom->createDocument(null, 'html', $doctype);
63 2
        $aframe_dom_head = $aframe_dom->createElement('head');
64 2
        $aframe_dom_body = $aframe_dom->createElement('body', "\n");
65 2
        $aframe_dom_scene = $aframe_dom->createElement("a-scene", "\n");
66
        
67
        /* Add metatags */
68 2
        $this->meta()->DOMAppendTags($aframe_dom, $aframe_dom_head);
69
        
70
        /* Add primitives to DOM */
71 2
        $this->DOMAppendPrimitives($aframe_dom, $aframe_dom_scene);
72
        
73 2
        $cdn_script = $aframe_dom->createElement('script');
74 2
        $cdn_script->setAttribute('src', 'https://aframe.io/releases/0.2.0/aframe.min.js');
75 2
        $aframe_dom_head->appendChild($cdn_script);
76
        
77
        /* Pull DOM together */
78 2
        $aframe_dom_body->appendChild($aframe_dom_scene);
79 2
        $html = $aframe_dom->getElementsByTagName('html')[0];
80 2
        $html->appendChild($aframe_dom_head);
81 2
        $html->appendChild($aframe_dom_body);
82
        
83 2
        $aframe_dom->formatOutput = true;
84
        
85
        /* Print Scene */
86
        
87 2
        if (! $full) {
88 1
            $html = new DOMDocument();
89 1
            $html_scene = $html->importNode($aframe_dom_scene, true);
90 1
            $html->appendChild($html_scene);
91 1
            if ($print)
92 1
                print $html->saveHTML();
93
            else
94 1
                return $html->saveHTML();
95
        } else {
96 2
            if ($print)
97 1
                print $aframe_dom->saveHTML();
98
            else
99 2
                return $aframe_dom->saveHTML();
100
        }
101 1
    }
102
}
103