Completed
Push — 0.3.x ( 1b67d9...e294c0 )
by Marko
03:01
created

MaterialComponent::initializeComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 25, 2016 - 7:51:42 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         MaterialComponent.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\Components\Material;
25
26
use \AframeVR\Interfaces\Core\Components\MaterialCMPTIF;
27
use \AframeVR\Core\Helpers\ComponentAbstract;
28
use \AframeVR\Core\Exceptions\BadShaderCallException;
29
use \AframeVR\Interfaces\ShaderInterface;
30
31
class MaterialComponent extends ComponentAbstract implements MaterialCMPTIF
32
{
33
34
    private $shaderObj;
35
36
    /**
37
     * Initialize Component
38
     *
39
     * {@inheritdoc}
40
     *
41
     * @return bool
42
     */
43 35
    public function initializeComponent(): bool
44
    {
45 35
        $this->setDomAttribute('material');
46 35
        return true;
47
    }
48
49
    /**
50
     * Return DOM attribute contents
51
     *
52
     * @return string
53
     */
54 3
    public function getDomAttributeString(): string
55
    {
56 3
        $this->prepareShader();
57 3
        return parent::getDomAttributeString();
58
    }
59
60
    /**
61
     * Material Shader
62
     *
63
     * {@inheritdoc}
64
     *
65
     * @param string $shader            
66
     * @throws BadShaderCallException
67
     * @return object|null
68
     */
69 16
    public function shader(string $shader = 'standard')
70
    {
71 16
        $this->dom_attributes['shader'] = $shader;
72
        
73 16
        if ($this->shaderObj instanceof ShaderInterface)
74 3
            return $this->shaderObj;
75
        
76 16
        $shader = sprintf('\AframeVR\Core\Shaders\%s', ucfirst($shader));
77 16
        if (class_exists($shader)) {
78 15
            $this->shaderObj = new $shader();
79
        } else {
80 1
            throw new BadShaderCallException($shader);
81
        }
82 15
        return $this->shaderObj ?? null;
83
    }
84
85
    /**
86
     * opacity
87
     *
88
     * {@inheritdoc}
89
     *
90
     * @param float $opacity            
91
     * @return void
92
     */
93 8
    public function opacity(float $opacity = 1.0)
94
    {
95 8
        $this->dom_attributes['opacity'] = $opacity;
96 8
    }
97
    
98
    /**
99
     * transparent
100
     *
101
     * {@inheritdoc}
102
     *
103
     * @param bool $transparent            
104
     * @return void
105
     */
106 7
    public function transparent(bool $transparent = false)
107
    {
108 7
        $this->dom_attributes['transparent'] = $transparent ? 'true' : 'false';
109 7
    }
110
    
111
    /**
112
     * depthTest
113
     *
114
     * {@inheritdoc}
115
     *
116
     * @param bool $depth_test
117
     * @return void
118
     */
119 1
    public function depthTest(bool $depth_test = true)
120
    {
121 1
        $this->dom_attributes['depthTest'] = $depth_test ? 'true' : 'false';
122 1
    }
123
    
124
    /**
125
     * side
126
     *
127
     * {@inheritdoc}
128
     *
129
     * @param string $side            
130
     * @return void
131
     */
132 1
    public function side(string $side = 'front')
133
    {
134 1
        $this->dom_attributes['side'] = $side;
135 1
    }
136
    
137
    /**
138
     * Prepare Shader attributes
139
     *
140
     * @return void
141
     */
142 3
    private function prepareShader()
143
    {
144 3
        if (! empty($this->shaderObj)) {
145 3
            $this->dom_attributes = array_merge($this->dom_attributes, $this->shaderObj->getAttributes());
146
        }
147 3
    }
148
}
149