Passed
Push — master ( 4f7cfa...de2995 )
by Gabriel
03:24
created

AssetRenderer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 42
c 1
b 0
f 1
dl 0
loc 118
ccs 43
cts 44
cp 0.9773
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareAttribsStyles() 0 9 2
A tag() 0 10 4
A for() 0 3 1
A prepareAttribs() 0 12 3
A prepareAttribsScripts() 0 8 2
A __construct() 0 3 1
A attribString() 0 10 1
A render() 0 13 1
1
<?php
2
3
namespace ByTIC\Assets\Assets\Asset;
4
5
use ByTIC\Assets\Assets\Asset;
6
use Nip\Utility\Arr;
7
8
/**
9
 * Class AssetRenderer
10
 * @package ByTIC\Assets\Assets\Asset
11
 */
12
class AssetRenderer
13
{
14
    /**
15
     * @var Asset
16
     */
17
    protected $asset;
18
19
    /**
20
     * AssetRenderer constructor.
21
     * @param Asset $asset
22
     */
23 4
    protected function __construct(Asset $asset)
24
    {
25 4
        $this->asset = $asset;
26 4
    }
27
28
    /**
29
     * @param Asset|Asset\Traits\CanRenderTrait $asset
30
     * @return string
31
     */
32 4
    public static function for(Asset $asset)
33
    {
34 4
        return (new static($asset))->render();
35
    }
36
37
    /**
38
     * @return string
39
     */
40 4
    protected function render()
41
    {
42 4
        $tag = $this->tag();
43 4
        $attributes = $this->attribString();
44 4
        $content = $this->asset->getContent();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $content is correct as $this->asset->getContent() targeting ByTIC\Assets\Assets\Asset::getContent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
        $output =
46 4
            sprintf(
47 4
                $tag,
48
                $attributes,
49
                $content
50
            );
51
52 4
        return $output;
53
    }
54
55
    /**
56
     * @return string
57
     */
58 4
    protected function tag()
59
    {
60 4
        if ($this->asset->getType() == Asset::TYPE_STYLES && $this->asset->hasContent()) {
61 1
            return '<style %s>%s</style>';
62
        }
63 3
        if ($this->asset->getType() == Asset::TYPE_SCRIPTS) {
64 1
            return '<script %s>%s</script>';
65
        }
66
67 2
        return '<link %s />';
68
    }
69
70
    /**
71
     * @return string
72
     */
73 4
    protected function attribString()
74
    {
75 4
        $attributesMap = $this->prepareAttribs();
76
77 4
        return implode(' ', array_map(
78
            function ($key, $value) {
79 4
                return sprintf('%s="%s"', $key, htmlentities($value));
80 4
            },
81 4
            array_keys($attributesMap),
82
            $attributesMap
83
        ));
84
    }
85
86
    /**
87
     * @return array
88
     */
89 4
    protected function prepareAttribs()
90
    {
91 4
        $attributesMap = $this->asset->getAttribs();
92 4
        if (!$this->asset->getType()) {
93
            return $attributesMap;
94
        }
95 4
        $method = 'prepareAttribs'.ucfirst(strtolower($this->asset->getType()));
96 4
        if (method_exists($this, $method)) {
97 4
            $attributesMap = $this->{$method}($attributesMap);
98
        }
99
100 4
        return $attributesMap;
101
    }
102
103
    /**
104
     * @param array $attributes
105
     * @return array
106
     */
107 3
    protected function prepareAttribsStyles($attributes)
108
    {
109 3
        unset($attributes['href']);
110 3
        $source = $this->asset->getSource();
111 3
        if ($source) {
112 2
            $attributes = Arr::prepend($attributes, $this->asset->getSource(), 'href');
113
        }
114
115 3
        return $attributes;
116
    }
117
118
    /**
119
     * @param array $attributes
120
     * @return array
121
     */
122 1
    protected function prepareAttribsScripts($attributes)
123
    {
124 1
        unset($attributes['src']);
125 1
        $source = $this->asset->getSource();
126 1
        if ($source) {
127 1
            $attributes = Arr::prepend($attributes, $this->asset->getSource(), 'src');
128
        }
129 1
        return $attributes;
130
    }
131
}