StaticManager::drawHeaderScripts()   A
last analyzed

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