Completed
Push — master ( f8571a...f0770d )
by Marko
04:14
created

Scene::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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\Extras\Primitives;
27
use \AframeVR\Core\Entity;
28
use \AframeVR\Core\DOM\AframeDOMDocument;
29
use \AframeVR\Interfaces\AssetsInterface;
30
31
final class Scene
32
{
33
    /* All scenes can use primitives */
34
    use Primitives;
35
36
    /**
37
     * Name with what you can retrieve this scene while working with multiple scenes
38
     *
39
     * @var string $name
40
     */
41
    private $name;
42
43
    protected $assets = array();
44
45
    /**
46
     * Aframe Document Object Model
47
     *
48
     * @var \AframeVR\Core\DOM\AframeDOMDocument
49
     */
50
    protected $aframeDomObj;
51
52
    /**
53
     * A-Frame scene entities
54
     *
55
     * @var array $entities
56
     */
57
    protected $entities = array();
58
59
    /**
60
     * Scene constructor
61
     *
62
     * @param string $name            
63
     * @param Config $config            
64
     */
65 59
    public function __construct(string $name, Config $config)
66
    {
67 59
        $this->name = $name;
68 59
        $this->aframeDomObj = new AframeDOMDocument($config);
69 59
    }
70
71
    /**
72
     * Aframe Document Object Model
73
     *
74
     * @api
75
     *
76
     * @return \AframeVR\Core\DOM\AframeDOMDocument
77
     */
78 4
    public function dom()
79
    {
80 4
        return $this->aframeDomObj;
81
    }
82
83
    /**
84
     * Entity
85
     *
86
     * @api
87
     *
88
     * @param string $name            
89
     * @return Entity
90
     */
91 27
    public function entity(string $name = 'untitled'): Entity
92
    {
93 27
        return $this->entities[$name] ?? $this->entities[$name] = new Entity();
94
    }
95
96
    /**
97
     * Assets
98
     *
99
     * @api
100
     *
101
     * @param string $name            
102
     * @return \AframeVR\Interfaces\AssetsInterface
103
     */
104 2
    public function asset(string $name = 'untitled'): AssetsInterface
105
    {
106 2
        return $this->assets[$name] ?? $this->assets[$name] = new Asset();
107
    }
108
109
    /**
110
     * Render the A-Frame scene and return the HTML
111
     *
112
     * @api
113
     *
114
     * @param bool $only_scene            
115
     * @return string
116
     */
117 3
    public function save($only_scene = false): string
118
    {
119 3
        $this->prepare();
120 3
        return ! $only_scene ? $this->aframeDomObj->render() : $this->aframeDomObj->renderSceneOnly();
121
    }
122
123
    /**
124
     * Render the A-Frame scene and passthru HTML
125
     *
126
     * @api
127
     *
128
     * @param bool $only_scene            
129
     * @return void
130
     */
131 1
    public function render($only_scene = false)
132
    {
133 1
        $this->prepare();
134 1
        print ! $only_scene ? $this->aframeDomObj->render() : $this->aframeDomObj->renderSceneOnly();
135 1
    }
136
137
    /**
138
     * Alias of AframeDOMDocument::setTitle(string)
139
     *
140
     * Set <title> tag
141
     * $this->dom()->setTitle(string);
142
     *
143
     * @api
144
     *
145
     * @param string $title            
146
     */
147 2
    public function title(string $title)
148
    {
149 2
        $this->dom()->setTitle($title);
150 2
    }
151
152
    /**
153
     * Alias of AframeDOMDocument::setDescription(string)
154
     *
155
     * Set <meta name="description"> tag
156
     * $this->dom()->setDescription(string);
157
     *
158
     * @api
159
     *
160
     * @param string $description            
161
     */
162 2
    public function description(string $description)
163
    {
164 2
        $this->dom()->setDescription($description);
165 2
    }
166
167
    /**
168
     * Add everyting to DOM
169
     *
170
     * @return void
171
     */
172 3
    protected function prepare()
173
    {
174
        /* Append all primitives */
175 3
        $this->preparePrimitives();
176 3
        $this->aframeDomObj->appendEntities($this->entities);
177 3
    }
178
}
179