Completed
Push — master ( a9c056...507e84 )
by Marko
03:27
created

MaterialComponent::prepareShader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2
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\{
29
    BadShaderCallException,
30
    InvalidShaderMethodException
31
};
32
use \AframeVR\Interfaces\ShaderInterface;
33
34
class MaterialComponent extends ComponentAbstract implements MaterialCMPTIF
35
{
36
    private $shaderObj;
37
38
    /**
39
     * Initialize Component
40
     *
41
     * {@inheritdoc}
42
     *
43
     * @return bool
44
     */
45 26
    public function initializeComponent(): bool
46
    {
47 26
        $this->setDomAttribute('material');
48 26
        return true;
49
    }
50
51
    /**
52
     * Return DOM attribute contents
53
     *
54
     * @return string
55
     */
56 7
    public function getDomAttributeString(): string
57
    {
58 7
        $this->prepareShader();
59 7
        return parent::getDomAttributeString();
60
    }
61
62
    /**
63
     * Call passes all calls to no existing methods to self::methodProvider
64
     *
65
     * @param string $method            
66
     * @param array $args            
67
     * @throws InvalidComponentMethodException
68
     */
69 12
    public function __call(string $method, $args)
70
    {
71
        /* Well then this call should be passed to shader, but lets make sure
72
         * that shader is loaded and let shader either to throw any throwable */
73 12
        $this->shader();
74 12
        if (method_exists($this->shaderObj, $method)) {
75 11
            call_user_func_array(
76
                array(
77 11
                    $this->shaderObj,
78 11
                    (string) $method
79
                ), $args);
80 11
            return $this;
81
        } else {
82 1
            $class = get_class($this->shaderObj);
83 1
            throw new InvalidShaderMethodException($method, $class);
84
        }
85
    }
86
87
    /**
88
     * Material Shader
89
     *
90
     * {@inheritdoc}
91
     *
92
     * @param null|string $shader            
93
     * @throws BadShaderCallException
94
     * @return ShaderInterface|MaterialComponent
95
     */
96 21
    public function shader(string $shader = null)
97
    {
98 21
        $this->dom_attributes['shader'] = $this->dom_attributes['shader'] ?? $shader ?? 'standard';
99
        
100 21
        if ($this->shaderObj instanceof ShaderInterface)
101 19
            return $this->shaderObj;
102
        
103 21
        $shader = sprintf('\AframeVR\Core\Shaders\%s', ucfirst($this->dom_attributes['shader']));
104 21
        if (class_exists($shader)) {
105 20
            $this->shaderObj = new $shader();
106
        } else {
107 1
            throw new BadShaderCallException($shader);
108
        }
109 20
        return $this;
110
    }
111
112
    /**
113
     * opacity
114
     *
115
     * {@inheritdoc}
116
     *
117
     * @param float $opacity            
118
     * @return MaterialCMPTIF
119
     */
120 11
    public function opacity(float $opacity = 1.0): MaterialCMPTIF
121
    {
122 11
        $this->dom_attributes['opacity'] = $opacity;
123 11
        return $this;
124
    }
125
126
    /**
127
     * transparent
128
     *
129
     * {@inheritdoc}
130
     *
131
     * @param bool $transparent            
132
     * @return MaterialCMPTIF
133
     */
134 10
    public function transparent(bool $transparent = false): MaterialCMPTIF
135
    {
136 10
        $this->dom_attributes['transparent'] = $transparent ? 'true' : 'false';
137 10
        return $this;
138
    }
139
140
    /**
141
     * depthTest
142
     *
143
     * {@inheritdoc}
144
     *
145
     * @param bool $depth_test            
146
     * @return MaterialCMPTIF
147
     */
148 1
    public function depthTest(bool $depth_test = true): MaterialCMPTIF
149
    {
150 1
        $this->dom_attributes['depthTest'] = $depth_test ? 'true' : 'false';
151 1
        return $this;
152
    }
153
154
    /**
155
     * side
156
     *
157
     * {@inheritdoc}
158
     *
159
     * @param string $side            
160
     * @return MaterialCMPTIF
161
     */
162 7
    public function side(string $side = 'front'): MaterialCMPTIF
163
    {
164 7
        $this->dom_attributes['side'] = $side;
165 7
        return $this;
166
    }
167
168
    /**
169
     * Do not apply fog to certain entities, we can disable fog for certain materials.
170
     *
171
     * @param bool $fog            
172
     * @return MaterialCMPTIF
173
     */
174 1
    public function fog(bool $fog = true): MaterialCMPTIF
175
    {
176 1
        $this->dom_attributes['fog'] = $fog ? 'true' : 'false';
177 1
        return $this;
178
    }
179
180
    /**
181
     * Prepare Shader attributes
182
     *
183
     * @return void
184
     */
185 7
    private function prepareShader()
186
    {
187 7
        if (! empty($this->shaderObj)) {
188 7
            $this->dom_attributes = array_merge($this->dom_attributes, $this->shaderObj->getAttributes());
189
        }
190 7
    }
191
}
192