Completed
Push — master ( e8b22d...bec3f6 )
by Marko
11s
created

MaterialComponent::__call()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 12
nc 6
nop 2
crap 5
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 it should be passed to shader, but lets make sure
72
         * that shader is loaded and let shader either to throw any throwable */
73 12
        (is_object($this->shaderObj) ?: $this->shader());
74
        
75 12
        if (is_object($this->shaderObj) && method_exists($this->shaderObj, $method)) {
76 11
            call_user_func_array(
77
                array(
78 11
                    $this->shaderObj,
79 11
                    (string) $method
80
                ), $args);
81 11
            return $this;
82
        } else {
83 1
            $class = is_object($this->shaderObj) ? get_class($this->shaderObj) : get_called_class();
84 1
            throw new InvalidShaderMethodException($method, $class);
85
        }
86
    }
87
88
    /**
89
     * Material Shader
90
     *
91
     * {@inheritdoc}
92
     *
93
     * @param null|string $shader            
94
     * @throws BadShaderCallException
95
     * @return object|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ShaderInterface|MaterialComponent.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
96
     */
97 21
    public function shader(string $shader = null)
98
    {
99 21
        $this->dom_attributes['shader'] = $this->dom_attributes['shader'] ?? $shader ?? 'standard';
100
        
101 21
        if ($this->shaderObj instanceof ShaderInterface)
102 12
            return $this->shaderObj;
103
        
104 21
        $shader = sprintf('\AframeVR\Core\Shaders\%s', ucfirst($this->dom_attributes['shader']));
105 21
        if (class_exists($shader)) {
106 20
            $this->shaderObj = new $shader();
107
        } else {
108 1
            throw new BadShaderCallException($shader);
109
        }
110 20
        return $this;
111
    }
112
113
    /**
114
     * opacity
115
     *
116
     * {@inheritdoc}
117
     *
118
     * @param float $opacity            
119
     * @return MaterialCMPTIF
120
     */
121 11
    public function opacity(float $opacity = 1.0): MaterialCMPTIF
122
    {
123 11
        $this->dom_attributes['opacity'] = $opacity;
124 11
        return $this;
125
    }
126
127
    /**
128
     * transparent
129
     *
130
     * {@inheritdoc}
131
     *
132
     * @param bool $transparent            
133
     * @return MaterialCMPTIF
134
     */
135 10
    public function transparent(bool $transparent = false): MaterialCMPTIF
136
    {
137 10
        $this->dom_attributes['transparent'] = $transparent ? 'true' : 'false';
138 10
        return $this;
139
    }
140
141
    /**
142
     * depthTest
143
     *
144
     * {@inheritdoc}
145
     *
146
     * @param bool $depth_test            
147
     * @return MaterialCMPTIF
148
     */
149 1
    public function depthTest(bool $depth_test = true): MaterialCMPTIF
150
    {
151 1
        $this->dom_attributes['depthTest'] = $depth_test ? 'true' : 'false';
152 1
        return $this;
153
    }
154
155
    /**
156
     * side
157
     *
158
     * {@inheritdoc}
159
     *
160
     * @param string $side            
161
     * @return MaterialCMPTIF
162
     */
163 7
    public function side(string $side = 'front'): MaterialCMPTIF
164
    {
165 7
        $this->dom_attributes['side'] = $side;
166 7
        return $this;
167
    }
168
169
    /**
170
     * Do not apply fog to certain entities, we can disable fog for certain materials.
171
     *
172
     * @param bool $fog            
173
     * @return MaterialCMPTIF
174
     */
175 1
    public function fog(bool $fog = true): MaterialCMPTIF
176
    {
177 1
        $this->dom_attributes['fog'] = $fog ? 'true' : 'false';
178 1
        return $this;
179
    }
180
181
    /**
182
     * Prepare Shader attributes
183
     *
184
     * @return void
185
     */
186 7
    private function prepareShader()
187
    {
188 7
        if (! empty($this->shaderObj)) {
189 7
            $this->dom_attributes = array_merge($this->dom_attributes, $this->shaderObj->getAttributes());
190
        }
191 7
    }
192
}
193