Completed
Pull Request — master (#66)
by Marko
06:34
created

Mixin::attr()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 12
nc 4
nop 2
crap 4
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
    public function attr(string $component_name, string $attr_data = null)
55 2
    {
56
        if(!is_null($attr_data)) {
57 2
            $this->attrs[$component_name] = $attr_data;
58 2
            return $this;
59
        }
60 2
        if (! array_key_exists($component_name, $this->components)) {
61 1
            $component = sprintf('\AframeVR\Core\Components\%s\%sComponent', ucfirst($component_name),
62
                ucfirst($component_name));
63 1
            if (class_exists($component)) {
64
                $this->components[$component_name] = new $component();
65
            } else {
66
                throw new BadComponentCallException($component_name);
67 1
            }
68
        }
69
70
        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 1
     * @param string $component_name
81
     * @param array $args
82 1
     */
83 1
    public function __call(string $component_name, array $args)
84 1
    {
85 1
        return $this->attr($component_name, $args[0] ?? null);
86 1
    }
87
88
    /**
89 1
     * material.color
90
     *
91
     * @param string $color
92
     * @return Mixin
93
     */
94
    public function color(string $color = 'gray')
95
    {
96
        $this->attr('Material')
97
        ->shader()
98 1
        ->color($color);
99
        return $this;
100 1
    }
101 1
102 1
    /**
103 1
     * material.metalness
104
     *
105
     * @param int|float $metalness
106
     * @return Mixin
107
     */
108
    public function metalness(float $metalness = 0)
109
    {
110
        $this->attr('Material')
111
        ->shader()
112 1
        ->metalness($metalness);
113
        return $this;
114 1
    }
115 1
116 1
    /**
117 1
     * material.roughness
118
     *
119
     * @param float $roughness
120
     * @return Mixin
121
     */
122
    public function roughness(float $roughness = 0.5)
123
    {
124
        $this->attr('Material')
125
        ->shader()
126 1
        ->roughness($roughness);
127
        return $this;
128 1
    }
129 1
130 1
    /**
131 1
     * material.src
132
     *
133
     * @param null|string $src
134
     * @return Mixin
135
     */
136
    public function src(string $src = null)
137
    {
138
        $this->attr('Material')
139
        ->shader()
140 1
        ->src($src);
141
        return $this;
142 1
    }
143 1
144 1
    /**
145 1
     * material.shader
146
     *
147
     * @param string $shader
148
     * @return Mixin
149
     */
150
    public function shader($shader = 'standard')
151
    {
152
        $this->attr('Material')->shader($shader);
153
        return $this;
154 1
    }
155
156 1
    /**
157 1
     * material.opacity
158
     *
159
     * @param float $opacity
160
     * @return Mixin
161
     */
162
    public function opacity(float $opacity = 1.0)
163
    {
164
        $this->attr('Material')->opacity($opacity);
165
        return $this;
166 1
    }
167
168 1
    /**
169 1
     * material.transparent
170
     *
171
     * @param bool $transparent
172
     * @return Mixin
173
     */
174
    public function transparent(bool $transparent = false)
175
    {
176
        $this->attr('Material')->transparent($transparent);
177
        return $this;
178 1
    }
179
}
180