Completed
Push — master ( 5f3088...bfb206 )
by Marko
02:40
created

Scene::assets()   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 1
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
use \AframeVR\Core\Entity;
31
use \AframeVR\Interfaces\AssetsInterface;
32
33
final class Scene
34
{
35
    use Primitives;
36
37
    private $name;
38
39
    protected $assets = array();
40
41
    /**
42
     * A-Frame scene entities
43
     *
44
     * @var array|null $entities
45
     */
46
    protected $entities;
47
48 56
    public function __construct($name)
49
    {
50 56
        $this->name = $name;
51 56
    }
52
53
    /**
54
     * Scene DOMDocument meta tags object
55
     *
56
     * @return \AframeVR\Core\Helpers\MetaTags
57
     */
58 7
    public function meta()
59
    {
60 7
        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...
61
    }
62
63
    /**
64
     * Entity
65
     *
66
     * @param string $name            
67
     * @return Entity
68
     */
69 26
    public function entity(string $name = 'untitled'): Entity
70
    {
71 26
        return $this->entities[$name] ?? $this->entities[$name] = new Entity();
72
    }
73
74
    /**
75
     * Assets
76
     *
77
     * @param string $name            
78
     * @return AssetsnInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be AssetsInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
     */
80 2
    public function assets(string $name = 'untitled'): AssetsInterface
81
    {
82 2
        return $this->assets[$name] ?? $this->assets[$name] = new Assets();
83
    }
84
85
    /**
86
     * Render the A-Frame scene
87
     *
88
     * @param bool $full            
89
     * @param bool $print            
90
     */
91 6
    public function render($full = true, $print = true)
92
    {
93 6
        $dom = new DOMImplementation();
94 6
        $doctype = $dom->createDocumentType('html');
95 6
        $aframe_dom = $dom->createDocument(null, 'html', $doctype);
96 6
        $aframe_dom_head = $aframe_dom->createElement('head');
97 6
        $aframe_dom_body = $aframe_dom->createElement('body', "\n");
98 6
        $aframe_dom_scene = $aframe_dom->createElement("a-scene", "\n");
99
        
100
        /* Add metatags */
101 6
        $this->meta()->DOMAppendTags($aframe_dom, $aframe_dom_head);
102
        
103
        /* Add primitives to DOM */
104 6
        $this->DOMAppendPrimitives($aframe_dom, $aframe_dom_scene);
105
        
106
        /* Add entities */
107 6
        if (is_array($this->entities)) {
108 4
            foreach ($this->entities as $entity) {
109 4
                $entity_dom = $entity->DOMElement($aframe_dom);
110 4
                $aframe_dom_scene->appendChild($entity_dom);
111
            }
112
        }
113
        
114 6
        $cdn_script = $aframe_dom->createElement('script');
115 6
        $cdn_script->setAttribute('src', 'https://aframe.io/releases/0.2.0/aframe.min.js');
116 6
        $aframe_dom_head->appendChild($cdn_script);
117
        
118
        /* Pull DOM together */
119 6
        $aframe_dom_body->appendChild($aframe_dom_scene);
120 6
        $html = $aframe_dom->getElementsByTagName('html')[0];
121 6
        $html->appendChild($aframe_dom_head);
122 6
        $html->appendChild($aframe_dom_body);
123
        
124 6
        $aframe_dom->formatOutput = true;
125
        
126
        /* Print Scene */
127
        
128 6
        if (! $full) {
129 1
            $html = new DOMDocument();
130 1
            $html_scene = $html->importNode($aframe_dom_scene, true);
131 1
            $html->appendChild($html_scene);
132 1
            if ($print)
133 1
                print $html->saveHTML();
134
            else
135 1
                return $html->saveHTML();
136
        } else {
137 6
            if ($print)
138 1
                print $aframe_dom->saveHTML();
139
            else
140 6
                return $aframe_dom->saveHTML();
141
        }
142 1
    }
143
}
144