Completed
Push — master ( e49eb8...84c8d2 )
by Marko
02:19
created

Primitives::sky()   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 - 10:21:07 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         Primitives.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\Extras;
25
26
use \AframeVR\Extras\Primitives\{
27
    Sphere,
28
    Box,
29
    Cylinder,
30
    Plane,
31
    Sky
32
};
33
use \AframeVR\Core\Entity;
34
35
trait Primitives
36
{
37
38
    /**
39
     *
40
     * @var array $spheres
41
     */
42
    protected $spheres = array();
43
44
    /**
45
     *
46
     * @var array $boxes
47
     */
48
    protected $boxes = array();
49
50
    /**
51
     *
52
     * @var array $cylinders
53
     */
54
    protected $cylinders = array();
55
56
    /**
57
     *
58
     * @var array $planes
59
     */
60
    protected $planes = array();
61
62
    /**
63
     *
64
     * @var \AframeVR\Extras\Primitives\Sky $sky
65
     */
66
    protected $sky;
67
68
    /**
69
     * A-Frame Primitive box
70
     *
71
     * @param string $name            
72
     * @return Entity
73
     */
74 9
    public function box(string $name = 'untitled'): Entity
75
    {
76 9
        return $this->boxes[$name] ?? $this->boxes[$name] = new Box();
77
    }
78
79
    /**
80
     * A-Frame Primitive sphere
81
     *
82
     * @param string $name            
83
     * @return Entity
84
     */
85 5
    public function sphere(string $name = 'untitled'): Entity
86
    {
87 5
        return $this->spheres[$name] ?? $this->spheres[$name] = new Sphere();
88
    }
89
90
    /**
91
     * A-Frame Primitive cylinder
92
     *
93
     * @param string $name            
94
     * @return Entity
95
     */
96 5
    public function cylinder(string $name = 'untitled'): Entity
97
    {
98 5
        return $this->cylinders[$name] ?? $this->cylinders[$name] = new Cylinder();
99
    }
100
101
    /**
102
     * A-Frame Primitive plane
103
     *
104
     * @param string $name            
105
     * @return Entity
106
     */
107 5
    public function plane(string $name = 'untitled'): Entity
108
    {
109 5
        return $this->planes[$name] ?? $this->planes[$name] = new Plane();
110
    }
111
112
    /**
113
     * A-Frame Primitive sky
114
     *
115
     * @return Entity
116
     */
117 5
    public function sky(): Entity
118
    {
119 5
        return $this->sky = new Sky();
120
    }
121
122
    /**
123
     * Add all used primitevs to the scene
124
     *
125
     * @param \DOMDocument $aframe_dom            
126
     * @param \DOMElement $scene            
127
     * @return void
128
     */
129 6
    protected function DOMAppendPrimitives(\DOMDocument &$aframe_dom, \DOMElement &$scene)
130
    {
131
        /* Add sphiers to DOM */
132 6
        $this->DOMappendPrimitiveCollection($this->spheres, $aframe_dom, $scene);
133
        
134
        /* Add boxes to DOM */
135 6
        $this->DOMappendPrimitiveCollection($this->boxes, $aframe_dom, $scene);
136
        
137
        /* Add cylinders to DOM */
138 6
        $this->DOMappendPrimitiveCollection($this->cylinders, $aframe_dom, $scene);
139
        
140
        /* Add planes to DOM */
141 6
        $this->DOMappendPrimitiveCollection($this->planes, $aframe_dom, $scene);
142
        
143
        /* Add sky to DOM */
144 6
        $this->DOMappendPrimitiveSky($aframe_dom, $scene);
145 6
    }
146
147
    /**
148
     * Append Primitive Collection
149
     *
150
     * @param array $primitives            
151
     * @param \DOMDocument $aframe_dom            
152
     * @param \DOMElement $scene            
153
     * @return void
154
     */
155 6
    private function DOMappendPrimitiveCollection(array $primitives, \DOMDocument &$aframe_dom, \DOMElement &$scene)
156
    {
157 6
        if (! empty($primitives)) {
158 1
            foreach ($primitives as $primitive) {
159 1
                $entity = $primitive->DOMElement($aframe_dom);
160 1
                $scene->appendChild($entity);
161
            }
162
        }
163 6
    }
164
165
    /**
166
     * Append SKY primitive
167
     *
168
     * @param \DOMDocument $aframe_dom            
169
     * @param \DOMElement $scene            
170
     * @return void
171
     */
172 6
    private function DOMappendPrimitiveSky(\DOMDocument &$aframe_dom, \DOMElement &$scene)
173
    {
174 6
        if (is_object($this->sky)) {
175 1
            $sky = $this->sky->DOMElement($aframe_dom);
176 1
            $scene->appendChild($sky);
177
        }
178 6
    }
179
}
180