Passed
Push — master ( d1c5a6...bac8bf )
by Fabian
03:36 queued 12s
created

ViewHelperStrategy   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A helper() 0 12 3
A setupAsset() 0 15 5
A str() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\AsseticBundle\View;
6
7
use Assetic\Asset\AssetCollection;
8
use Laminas\View\Renderer\PhpRenderer;
9
use Assetic\Contracts\Asset\AssetInterface;
10
11
class ViewHelperStrategy extends AbstractStrategy
12
{
13
14
    public function setupAsset(AssetInterface $asset): void
15
    {
16
        $baseUrl  = $this->str($this->getBaseUrl());
17
        $basePath = $this->str($this->getBasePath());
18
19
        if ($this->isDebug() && !$this->isCombine() && $asset instanceof AssetCollection) {
20
            // Move assets as single instance not as a collection
21
            /** @var AssetCollection $value */
22
            foreach ($asset as $value) {
23
                $path = $baseUrl . $basePath . $this->str($value->getTargetPath());
24
                $this->helper($path);
25
            }
26
        } else {
27
            $path = $baseUrl . $basePath . $this->str($asset->getTargetPath());
28
            $this->helper($path);
29
        }
30
    }
31
32
    private function str(?string $s): string
33
    {
34
        return $s ?? '';
35
    }
36
37
    protected function helper(string $path): void
38
    {
39
        $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($path, Fabiang\...iew\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $extension = strtolower(/** @scrutinizer ignore-type */ pathinfo($path, PATHINFO_EXTENSION));
Loading history...
40
41
        switch ($extension) {
42
            case 'js':
43
                $this->appendScript($path);
44
                break;
45
46
            case 'css':
47
                $this->appendStylesheet($path);
48
                break;
49
        }
50
    }
51
52
    protected function appendScript(string $path): void
53
    {
54
        $renderer = $this->getRenderer();
55
        switch (true) {
56
            case ($renderer instanceof PhpRenderer):
57
                if (strpos($path, "head_") !== false) {
58
                    /** @var \Laminas\View\Helper\HeadScript $headScript */
59
                    $headScript = $renderer->plugin('HeadScript');
60
                    $headScript->appendFile($path);
61
                } else {
62
                    /** @var \Laminas\View\Helper\InlineScript $inlineScript */
63
                    $inlineScript = $renderer->plugin('InlineScript');
64
                    $inlineScript->appendFile($path);
65
                }
66
                break;
67
        }
68
    }
69
70
    protected function appendStylesheet(string $path): void
71
    {
72
        $renderer = $this->getRenderer();
73
        switch (true) {
74
            case ($renderer instanceof PhpRenderer):
75
                /** @var \Laminas\View\Helper\HeadLink $headLink */
76
                $headLink = $renderer->plugin('HeadLink');
77
                $headLink->appendStylesheet($path);
78
                break;
79
        }
80
    }
81
82
}
83