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

AssetsAbstract::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 28, 2016 - 3:47:35 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         AssetsAbstract.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\AssetsInterface;
27
use \DOMElement;
28
29
abstract class AssetsAbstract implements AssetsInterface
30
{
31
32
    /**
33
     * DOM tag name of asset item
34
     *
35
     * @var string
36
     */
37
    protected $element_tag = 'a-asset-item';
38
    protected $attrs = array();
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...
39
    protected $components = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
40
    protected $assets_uri;
41
42
    /**
43
     * Asset constructor set asset ID
44
     *
45
     * @param string $id
46
     */
47 7
    public function __construct(string $id, string $assets_uri)
48
    {
49 7
        $this->id($id);
50 7
        $this->assets_uri = $assets_uri;
51 7
        $this->init();
52 7
    }
53
54 4
    public function init()
55
    {
56 4
    }
57
58
    /**
59
     * Set ID attribute of the asset
60
     *
61
     * {@inheritdoc}
62
     *
63
     * @param string $id
64
     * @return AssetsInterface
65
     */
66 7
    public function id(string $id = '0'): AssetsInterface
67
    {
68 7
        $this->attrs['id'] = $id;
69 7
        return $this;
70
    }
71
72
    /**
73
     * Set Assets src attribute
74
     *
75
     * {@inheritdoc}
76
     *
77
     * @param null|string $src
78
     * @return AssetsInterface
79
     */
80 1
    public function src(string $src = null): AssetsInterface
81
    {
82 1
        $this->attrs['src'] = $this->assets_uri ? $this->createURL($src) : $src;
83 1
        return $this;
84
    }
85
86
    /**
87
     * Create and add DOM element of the asset
88
     *
89
     * @param \DOMDocument $aframe_dom
90
     * @return \DOMElement
91
     */
92 3
    public function domElement(\DOMDocument &$aframe_dom): DOMElement
93
    {
94 3
        $a_asset = $aframe_dom->createElement($this->element_tag);
95
        /* Asset must have a id */
96 3
        $this->appendAttributes($a_asset);
97
98 3
        foreach ($this->components as $component) {
99
            /*
100
             * Check does component has any attributes to add to DOM element.
101
             * default attributes most of cases are ommited so we might not have any attributes to add
102
             */
103 1
            if ($component->hasDOMAttributes())
104 1
                $a_asset->setAttributeNode($component->getDOMAttr());
105
        }
106
107 3
        return $a_asset;
108
    }
109
110
    /**
111
     * Set Dom element name
112
     *
113
     * @param string $element_tag
114
     * @return void
115
     */
116 3
    public function setDomElementTag(string $element_tag)
117
    {
118 3
        $this->element_tag = $element_tag;
119 3
    }
120
121
    /**
122
     * Create asset URL
123
     *
124
     * If it is remote resource use fully qualified
125
     * URL instead creating it relative to local assets url
126
     *
127
     * @param string $url
128
     * @return string
129
     */
130 1
    private function createURL(string $url)
131
    {
132 1
        return substr($url,0,4) === 'http' ? $url : $this->assets_uri . $url;
133
    }
134
135
    /**
136
     * Append DOM attributes no set by components
137
     *
138
     * @param \DOMElement $a_entity
139
     */
140 3
    private function appendAttributes(\DOMElement &$a_entity)
141
    {
142 3
        foreach ($this->attrs as $attr => $val) {
143 3
            $this->setAttribute($a_entity, $attr, $val);
144
        }
145 3
    }
146
147 3
    private function setAttribute(&$a_entity, $attr, $val)
148
    {
149 3
        if ($attr === 'id' && is_numeric($val))
150 3
            return;
151
152 3
        $a_entity->setAttribute($attr, $val);
153 3
    }
154
}
155