Completed
Push — 0.3.x ( 4f6fde...8ea901 )
by Marko
02:33
created

ComponentAbstract::__call()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
rs 8.8571
cc 5
eloc 12
nc 4
nop 2
crap 5.0187
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 25, 2016 - 7:55:29 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         ComponentAbstract.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\Helpers;
25
26
use \AframeVR\Interfaces\ComponentInterface;
27
use \AframeVR\Core\Exceptions\{
28
    InvalidComponentMethodException,
29
    InvalidComponentArgumentException
30
};
31
use \DOMAttr;
32
33
abstract class ComponentAbstract implements ComponentInterface
34
{
35
    /**
36
     * Array of dom attributes
37
     * 
38
     * @var array
39
     */
40
    protected $dom_attributes = array();
41
    /**
42
     * Array of component scripts
43
     * 
44
     * @var array
45
     */
46
    protected $component_scripts = array();
47
    /**
48
     * Name of DOM attribute
49
     * 
50
     * @var string
51
     */
52
    protected $dom_attribute_name;
53
    /**
54
     * Loaded Component Method object
55
     * 
56
     * @var object
57
     */
58
    protected $methodProvider;
59
60
    /**
61
     * Component Constructor
62
     */
63 64
    public function __construct()
64
    {
65 64
        $this->initializeComponent();
66 64
        $this->setMethodProvider();
67 64
    }
68
69
    /**
70
     * Call passes all calls to no existing methods to self::methodProvider
71
     * 
72
     * @param string $method
73
     * @param array $args
74
     * @throws InvalidComponentMethodException
75
     */
76 52
    public function __call( string $method, $args)
77
    {
78 52
        if (is_object($this->methodProvider) && method_exists($this->methodProvider, $method)) {
79 52
            array_unshift($args, 0);
80 52
            $args[0] = &$this->dom_attributes;
81 52
            return call_user_func_array(
82
                array(
83 52
                    $this->methodProvider,
84 52
                    (string) $method
85
                ), $args);
86 1
        } elseif (is_object($this->methodProvider)) {
87 1
            $class = is_object($this->methodProvider) ? get_class($this->methodProvider) : get_called_class();
88 1
            throw new InvalidComponentMethodException($method, $class);
89
        }
90
    }
91
92
    /**
93
     * Return DOM attribute contents
94
     * 
95
     * @return string
96
     */
97 6
    public function getDomAttributeString(): string
98
    {
99 6
        $attrs = $this->getDOMAttributesArray();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
100 6
        $attr_format = implode(': %s; ', array_keys($attrs)) . ': %s;';
101 6
        return vsprintf($attr_format, array_values($attrs));
102
    }
103
104
    /**
105
     * Set class providing component methods
106
     * 
107
     * @param string $mp
108
     * @return void
109
     */
110 64
    public function setMethodProvider( string $mp = 'DefaultMethods')
111
    {
112 64
        $mp_class = substr(get_called_class(), 0, strrpos(get_called_class(), '\\')) . '\Methods\\' . $mp;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
113 64
        $this->methodProvider = class_exists($mp_class) ? new $mp_class() : null;
114 64
    }
115
116
    /**
117
     * Does component have DOM Atributes
118
     * 
119
     * {@inheritdoc}
120
     *
121
     * @return bool
122
     */
123 4
    public function hasDOMAttributes(): bool
124
    {
125 4
        return ! empty($this->dom_attributes);
126
    }
127
128
    /**
129
     * Get component DOM Atributes array
130
     * 
131
     * {@inheritdoc}
132
     *
133
     * @return array
134
     */
135 22
    public function getDOMAttributesArray(): array
136
    {
137 22
        return $this->dom_attributes;
138
    }
139
140
    /**
141
     * Get Component scripts
142
     * 
143
     * {@inheritdoc}
144
     *
145
     * @return array
146
     */
147 2
    public function getComponentScripts(): array
148
    {
149 2
        return (array) $this->component_scripts;
150
    }
151
152
    /**
153
     * Add component scripts
154
     * 
155
     * {@inheritdoc}
156
     *
157
     */
158 1
    public function addComponentScripts( string $vendor_component, string $script_name)
159
    {
160 1
        $this->component_scripts[$vendor_component] = $script_name;
161 1
    }
162
163
    /**
164
     * Get DOMAttr for the entity
165
     * 
166
     * @return DOMAttr
167
     */
168 4
    public function getDOMAttr(): DOMAttr
169
    {
170 4
        return new DOMAttr($this->getDomAttributeName(), $this->getDomAttributeString());
171
    }
172
173
    /**
174
     * Get Dom attribute name
175
     * 
176
     * @return string
177
     */
178 4
    public function getDomAttributeName(): string
179
    {
180 4
        return $this->dom_attribute_name;
181
    }
182
183
    /**
184
     * Set Dom Attribute name
185
     * 
186
     * @param string $dom_attribute_name
187
     * @return void
188
     */
189 64
    public function setDomAttribute( string $dom_attribute_name)
190
    {
191 64
        $this->dom_attribute_name = $dom_attribute_name;
192 64
    }
193
}
194