Completed
Push — master ( ba5676...ecd6d3 )
by Alexpts
06:22
created

StaticManager::convertToAttributesString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\StaticManager;
5
6
use PTS\Tools\CollectionInterface;
7
use PTS\Tools\NotFoundKeyException;
8
use Symfony\Component\Asset\PackageInterface;
9
10
class StaticManager
11
{
12
    /** @var CollectionInterface[] */
13
    protected $collections;
14
    /** @var PackageInterface[] */
15
    protected $packages = [];
16 10
17
    public function __construct(CollectionInterface $emptyCollection)
18 10
    {
19 10
        $this->collections = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('css' => $emptyCol...clone $emptyCollection) of type array<string,object<PTS\...CollectionInterface>"}> is incompatible with the declared type array<integer,object<PTS...s\CollectionInterface>> of property $collections.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20 10
            'css' => $emptyCollection,
21 10
            'jsHeader' => clone $emptyCollection,
22
            'jsFooter' => clone $emptyCollection,
23 10
        ];
24
    }
25 3
26
    public function getCssSet(): CollectionInterface
27 3
    {
28
        return $this->collections['css'];
29
    }
30 3
31
    public function getJsHeaderSet(): CollectionInterface
32 3
    {
33
        return $this->collections['jsHeader'];
34
    }
35 3
36
    public function getJsFooterSet(): CollectionInterface
37 3
    {
38
        return $this->collections['jsFooter'];
39
    }
40 2
41
    public function drawHeaderScripts(): string
42 2
    {
43 2
        $collection = $this->getJsHeaderSet();
44
        return $this->drawScripts($collection);
45
    }
46 2
47
    public function drawFooterScripts(): string
48 2
    {
49 2
        $collection = $this->getJsFooterSet();
50
        return $this->drawScripts($collection);
51
    }
52 4
53
    protected function drawScripts(CollectionInterface $collection): string
54 4
    {
55
        $scripts = $collection->getFlatItems(true);
56 4
57 4
        $result = '';
58 4
        foreach ($scripts as $item) {
59
            $item = $this->prepareItem($item, 'src');
60
            $attributes = $this->convertToAttributesString($item);
61 4
            $result .= "<script {$attributes}></script>\n";
62
        }
63
64 2
        return $result;
65
    }
66 2
67 2
    protected function convertToAttributesString(array $item): string
68
    {
69 2
        return implode(' ', array_map(function ($val, $key) {
70 2
            return "{$key}='{$val}'";
71 2
        }, $item, array_keys($item)));
72
    }
73
74 2
    public function drawStyles(): string
75
    {
76
        $collection = $this->getCssSet();
77 1
        $styles = $collection->getFlatItems(true);
78
79 1
        $result = '';
80 1
        foreach ($styles as $item) {
81
            $item = $this->prepareItem($item, 'href');
82
83
            if (!array_key_exists('rel', $item)) {
84
                $item['rel'] = 'stylesheet';
85
            }
86
87
            $attributes = $this->convertToAttributesString($item);
88 2
            $result .= "<link {$attributes}/>\n";
89
        }
90 2
91 1
        return $result;
92
    }
93
94 1
    /**
95
     * @param string|array $item
96
     * @param string $defaultAttr
97
     * @return array
98
     */
99
    protected function prepareItem($item, $defaultAttr = 'src'): array
100
    {
101
        return is_string($item) ? [$defaultAttr => $item] : $item;
102
    }
103
104
    public function setPackage(string $name, PackageInterface $package)
105
    {
106
        $this->packages[$name] = $package;
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $name
112
     * @return PackageInterface
113
     * @throws NotFoundKeyException
114
     */
115
    public function getPackage(string $name): PackageInterface
116
    {
117
        if (!array_key_exists($name, $this->packages)) {
118
            throw new NotFoundKeyException('Package not found');
119
        }
120
121
        return $this->packages[$name];
122
    }
123
}
124