Completed
Push — master ( be4ea7...66bb0f )
by Marko
12s
created

Mixin::component()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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