Completed
Push — master ( f87c77...be4ea7 )
by Marko
02:11
created

Mixin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 153
ccs 43
cts 43
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A color() 0 7 1
A metalness() 0 7 1
A roughness() 0 7 1
A src() 0 7 1
A component() 0 14 3
A __call() 0 11 2
A shader() 0 5 1
A opacity() 0 5 1
A transparent() 0 5 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 20, 2016 - 9:12:11 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         Mixin.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\Assets;
25
26
use \AframeVR\Core\Exceptions\BadComponentCallException;
27
use \AframeVR\Interfaces\Core\Assets\MixinInterface;
28
use \AframeVR\Core\Helpers\AssetsAbstract;
29
use \Closure;
30
31
final class Mixin extends AssetsAbstract implements MixinInterface
32
{
33
34
    /**
35
     * DOM tag name of asset item
36
     *
37
     * @var string
38
     */
39
    protected $element_tag = 'a-mixin';
40
    
41
    /**
42
     * Array of mocked components
43
     *
44
     * @var array
45
     */
46
    protected $components = array();
47
48
    /**
49
     * Load component for this entity
50
     *
51
     * @param string $component_name            
52
     * @throws \AframeVR\Core\Exceptions\BadComponentCallException
53
     * @return object|null
54
     */
55 2
    public function component(string $component_name)
56
    {
57 2
        if (! array_key_exists($component_name, $this->components)) {
58 2
            $component = sprintf('\AframeVR\Core\Components\%s\%sComponent', ucfirst($component_name), 
59
                ucfirst($component_name));
60 2
            if (class_exists($component)) {
61 1
                $this->components[$component_name] = new $component();
62
            } else {
63 1
                throw new BadComponentCallException($component_name);
64
            }
65
        }
66
        
67 1
        return $this->components[$component_name] ?? null;
68
    }
69
    
70
    /**
71
     * Handle entity components
72
     *
73
     * Since we might need to customize these to have
74
     * custom components loaded as $this->methosd aswell therefore
75
     * we have these placeholder magic methods here
76
     *
77
     * @param string $component_name            
78
     * @param array $args            
79
     */
80 1
    public function __call(string $component_name, array $args)
81
    {
82 1
        if (! method_exists($this, $component_name)) {
83 1
            $this->{$component_name} = Closure::bind(
84 1
                function () use ($component_name) {
85 1
                    return $this->component($component_name);
86 1
                }, $this, get_class());
87
        }
88
        
89 1
        return call_user_func($this->{$component_name}, $args);
90
    }
91
    
92
    /**
93
     * material.color
94
     *
95
     * @param string $color
96
     * @return Mixin
97
     */
98 1
    public function color(string $color = 'gray')
99
    {
100 1
        $this->component('Material')
101 1
        ->shader()
102 1
        ->color($color);
103 1
        return $this;
104
    }
105
    
106
    /**
107
     * material.metalness
108
     *
109
     * @param int|float $metalness
110
     * @return Mixin
111
     */
112 1
    public function metalness(float $metalness = 0)
113
    {
114 1
        $this->component('Material')
115 1
        ->shader()
116 1
        ->metalness($metalness);
117 1
        return $this;
118
    }
119
    
120
    /**
121
     * material.roughness
122
     *
123
     * @param float $roughness
124
     * @return Mixin
125
     */
126 1
    public function roughness(float $roughness = 0.5)
127
    {
128 1
        $this->component('Material')
129 1
        ->shader()
130 1
        ->roughness($roughness);
131 1
        return $this;
132
    }
133
    
134
    /**
135
     * material.src
136
     *
137
     * @param null|string $src
138
     * @return Mixin
139
     */
140 1
    public function src(string $src = null)
141
    {
142 1
        $this->component('Material')
143 1
        ->shader()
144 1
        ->src($src);
145 1
        return $this;
146
    }
147
    
148
    /**
149
     * material.shader
150
     *
151
     * @param string $shader
152
     * @return Mixin
153
     */
154 1
    public function shader($shader = 'standard')
155
    {
156 1
        $this->component('Material')->shader($shader);
157 1
        return $this;
158
    }
159
    
160
    /**
161
     * material.opacity
162
     *
163
     * @param float $opacity
164
     * @return Mixin
165
     */
166 1
    public function opacity(float $opacity = 1.0)
167
    {
168 1
        $this->component('Material')->opacity($opacity);
169 1
        return $this;
170
    }
171
    
172
    /**
173
     * material.transparent
174
     *
175
     * @param bool $transparent
176
     * @return Mixin
177
     */
178 1
    public function transparent(bool $transparent = false)
179
    {
180 1
        $this->component('Material')->transparent($transparent);
181 1
        return $this;
182
    }
183
}
184