Completed
Push — master ( ad5651...3fe817 )
by Marko
02:53
created

Scene::save()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 6
nc 4
nop 2
crap 4
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\Core\Assets;
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 $keyword;
42
43
    /**
44
     * Is scene prepared for rendering
45
     *
46
     * @var bool
47
     */
48
    private $prepared;
49
50
    protected $assets;
51
52
    /**
53
     * Aframe Document Object Model
54
     *
55
     * @var \AframeVR\Core\DOM\AframeDOMDocument
56
     */
57
    protected $aframeDomObj;
58
59
    /**
60
     * A-Frame scene entities
61
     *
62
     * @var array $entities
63
     */
64
    protected $entities = array();
65
66
    /**
67
     * Scene constructor
68
     *
69
     * @param string $keyword            
70
     * @param Config $config            
71
     */
72 71
    public function __construct(string $keyword, Config $config)
73
    {
74 71
        $this->keyword = $keyword;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75 71
        $this->aframeDomObj = new AframeDOMDocument($config);
76
        /* Initialize assests manager */
77 71
        $this->asset();
78 71
    }
79
80
    /**
81
     * Aframe Document Object Model
82
     *
83
     * @api
84
     *
85
     * @return \AframeVR\Core\DOM\AframeDOMDocument
86
     */
87 8
    public function dom()
88
    {
89 8
        return $this->aframeDomObj;
90
    }
91
92
    /**
93
     * Entity
94
     *
95
     * @api
96
     *
97
     * @param string $name            
98
     * @return \AframeVR\Core\Entity
99
     */
100 31
    public function entity(string $name = 'untitled'): Entity
101
    {
102 31
        return $this->entities[$name] ?? $this->entities[$name] = new Entity();
103
    }
104
105
    /**
106
     * Assets
107
     *
108
     * @api
109
     *
110
     * @return \AframeVR\Core\Assets
111
     */
112 71
    public function asset(): Assets
113
    {
114 71
        return $this->assets ?? $this->assets = new Assets();
115
    }
116
117
    /**
118
     * Render the A-Frame scene and return the HTML
119
     *
120
     * @api
121
     *
122
     * @param bool $only_scene            
123
     * @return string
124
     */
125 7
    public function save($only_scene = false, string $file = null): string
126
    {
127 7
        $this->prepare();
128 7
        $html = ! $only_scene ? $this->aframeDomObj->render() : $this->aframeDomObj->renderSceneOnly();
129 7
        if (! empty($file) && is_writable(dirname($file))) {
130 1
            file_put_contents($file, $html);
131
        }
132
        
133 7
        return $html;
134
    }
135
136
    /**
137
     * Render the A-Frame scene and passthru HTML
138
     *
139
     * @api
140
     *
141
     * @param bool $only_scene            
142
     * @return void
143
     */
144 1
    public function render($only_scene = false)
145
    {
146 1
        print $this->save($only_scene);
147 1
    }
148
149
    /**
150
     * Alias of AframeDOMDocument::setTitle(string)
151
     *
152
     * Set <title> tag
153
     * $this->dom()->setTitle(string);
154
     *
155
     * @api
156
     *
157
     * @param string $title            
158
     */
159 7
    public function title(string $title)
160
    {
161 7
        $this->dom()->setTitle($title);
162 7
    }
163
164
    /**
165
     * Alias of AframeDOMDocument::setDescription(string)
166
     *
167
     * Set <meta name="description"> tag
168
     * $this->dom()->setDescription(string);
169
     *
170
     * @api
171
     *
172
     * @param string $description            
173
     */
174 7
    public function description(string $description)
175
    {
176 7
        $this->dom()->setDescription($description);
177 7
    }
178
179
    /**
180
     * Get scenes keyword
181
     * 
182
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

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...
183
     */
184 1
    public function getKeyword()
185
    {
186 1
        return $this->keyword;
187
    }
188
    
189
    /**
190
     * Add everyting to DOM
191
     *
192
     * @return void
193
     */
194 7
    protected function prepare()
195
    {
196 7
        if ($this->prepared)
197 1
            return;
198
            
199
            /* Append all assets */
200 7
        $assets = $this->assets->getAssets();
201 7
        (! $assets) ?: $this->aframeDomObj->appendAssets($assets);
202
        
203
        /* Append all primitives */
204 7
        $this->preparePrimitives();
205
        
206
        /* Append all entities */
207 7
        $this->aframeDomObj->appendEntities($this->entities);
208 7
        $this->prepared = true;
209 7
    }
210
}
211