MaterialComponent::shader()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 10

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 15
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 10
nc 3
nop 1
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 8
    public function getDomAttributeString(): string
57
    {
58 8
        $this->prepareShader();
59 8
        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
        /*
72
         * Well then this call should be passed to shader, but lets make sure
73
         * that shader is loaded and let shader either to throw any throwable
74
         */
75 12
        $this->shader();
76 12
        if (method_exists($this->shaderObj, $method)) {
77 11
            call_user_func_array(
78
                array(
79 11
                    $this->shaderObj,
80 11
                    (string) $method
81
                ), $args);
82 11
            return $this;
83
        } else {
84 1
            $class = get_class($this->shaderObj);
85 1
            throw new InvalidShaderMethodException($method, $class);
86
        }
87
    }
88
89
    /**
90
     * Material Shader
91
     *
92
     * {@inheritdoc}
93
     *
94
     * @param null|string $shader
0 ignored issues
show
Documentation introduced by
There is no parameter named $shader. Did you maybe mean $shader_name?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
95
     * @throws BadShaderCallException
96
     * @return ShaderInterface|MaterialComponent
97
     */
98 24
    public function shader(string $shader_name = null)
99
    {
100 24
        if ($this->shaderObj instanceof ShaderInterface && ($this->dom_attributes['shader'] === $shader_name || empty($shader_name)))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
101 20
            return $this->shaderObj;
102
103 24
        $this->dom_attributes['shader'] = $shader_name ?? $this->dom_attributes['shader'] ?? 'standard';
104
105 24
        $shader = sprintf('\AframeVR\Core\Shaders\%s', ucfirst($this->dom_attributes['shader']));
106 24
        if (class_exists($shader)) {
107 23
            $this->shaderObj = new $shader();
108
        } else {
109 1
            throw new BadShaderCallException($shader);
110
        }
111 23
        return $this;
112
    }
113
114
    /**
115
     * repeat
116
     *
117
     * @param float $x
118
     * @param float $y
119
     */
120 5
    public function repeat(float $x, float $y)
121
    {
122 5
        $this->dom_attributes['repeat'] = sprintf('%d %d', $x, $y);
123 5
        return $this;
124
    }
125
126
    /**
127
     * opacity
128
     *
129
     * {@inheritdoc}
130
     *
131
     * @param float $opacity
132
     * @return MaterialCMPTIF
133
     */
134 11
    public function opacity(float $opacity = 1.0): MaterialCMPTIF
135
    {
136 11
        $this->dom_attributes['opacity'] = $opacity;
137 11
        return $this;
138
    }
139
140
    /**
141
     * transparent
142
     *
143
     * {@inheritdoc}
144
     *
145
     * @param bool $transparent
146
     * @return MaterialCMPTIF
147
     */
148 11
    public function transparent(bool $transparent = false): MaterialCMPTIF
149
    {
150 11
        $this->dom_attributes['transparent'] = $transparent ? 'true' : 'false';
151 11
        return $this;
152
    }
153
154
    /**
155
     * depthTest
156
     *
157
     * {@inheritdoc}
158
     *
159
     * @param bool $depth_test
160
     * @return MaterialCMPTIF
161
     */
162 1
    public function depthTest(bool $depth_test = true): MaterialCMPTIF
163
    {
164 1
        $this->dom_attributes['depthTest'] = $depth_test ? 'true' : 'false';
165 1
        return $this;
166
    }
167
168
    /**
169
     * side
170
     *
171
     * {@inheritdoc}
172
     *
173
     * @param string $side
174
     * @return MaterialCMPTIF
175
     */
176 10
    public function side(string $side = 'front'): MaterialCMPTIF
177
    {
178 10
        $this->dom_attributes['side'] = $side;
179 10
        return $this;
180
    }
181
182
    /**
183
     * Do not apply fog to certain entities, we can disable fog for certain materials.
184
     *
185
     * @param bool $fog
186
     * @return MaterialCMPTIF
187
     */
188 1
    public function fog(bool $fog = true): MaterialCMPTIF
189
    {
190 1
        $this->dom_attributes['fog'] = $fog ? 'true' : 'false';
191 1
        return $this;
192
    }
193
194
    /**
195
     * roughness
196
     *
197
     * @param float $roughness
198
     * @return MaterialCMPTIF
199
     */
200 4
    public function roughness(float $roughness): MaterialCMPTIF
201
    {
202 4
        $this->dom_attributes['roughness'] = $roughness;
203 4
        return $this;
204
    }
205
206
    /**
207
     * Prepare Shader attributes
208
     *
209
     * @return void
210
     */
211 8
    private function prepareShader()
212
    {
213 8
        if (! empty($this->shaderObj)) {
214 8
            $this->dom_attributes = array_merge($this->dom_attributes, $this->shaderObj->getAttributes());
215
        }
216
217 8
    }
218
}
219