Asset::setupAsset()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 0
cts 9
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 2
nop 2
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\AsseticBundle\View\Helper;
6
7
use Assetic\Asset\AssetCollection;
8
use Assetic\Contracts\Asset\AssetInterface;
9
use Fabiang\AsseticBundle\Exception;
10
use Fabiang\AsseticBundle\Exception\InvalidArgumentException;
11
use Fabiang\AsseticBundle\Factory\ServiceFactory;
12
use Fabiang\AsseticBundle\Service;
13
use Laminas\View\Helper\Placeholder\Container;
14
use Psr\Container\ContainerInterface;
15
16
use function pathinfo;
17
use function strtolower;
18
19
use const PATHINFO_EXTENSION;
20
use const PHP_EOL;
21
22
/**
23
 * @psalm-suppress PropertyNotSetInConstructor Upstream issue with contructor
24
 * @psalm-suppress MissingTemplateParam
25
 */
26
class Asset extends Container\AbstractStandalone
27
{
28
    protected Service $service;
29
    protected ?string $baseUrl  = null;
30
    protected ?string $basePath = null;
31
32
    public function __construct(ContainerInterface $container)
33
    {
34
        $serviceFactory = new ServiceFactory();
35
        $this->service  = $serviceFactory($container, Service::class, []);
36
        $this->service->build();
37
38
        $this->baseUrl  = $this->service->getConfiguration()->getBaseUrl();
39
        $this->basePath = $this->service->getConfiguration()->getBasePath();
40
    }
41
42
    /**
43
     * @throws InvalidArgumentException
44
     */
45
    public function __invoke(string $collectionName, array $options = []): string
46
    {
47
        /**
48
         * @psalm-suppress UndefinedDocblockClass Upstram bug in `@return`
49
         */
50
        if (! $this->service->getAssetManager()->has($collectionName)) {
51
            throw new Exception\InvalidArgumentException(
52
                'Collection "' . $collectionName . '" does not exist.'
53
            );
54
        }
55
56
        $asset = $this->service->getAssetManager()->get($collectionName);
57
58
        return $this->setupAsset($asset, $options);
59
    }
60
61
    protected function setupAsset(AssetInterface $asset, array $options = []): string
62
    {
63
        $ret = '';
64
65
        if (
66
            $this->service->getConfiguration()->isDebug()
67
            && ! $this->service->getConfiguration()->isCombine()
68
            && $asset instanceof AssetCollection
69
        ) {
70
            // Move assets as single instance not as a collection
71
            /** @var AssetCollection $value */
72
            foreach ($asset as $value) {
73
                $ret .= $this->helper($value, $options) . PHP_EOL;
74
            }
75
        } else {
76
            $ret .= $this->helper($asset, $options) . PHP_EOL;
77
        }
78
79
        return $ret;
80
    }
81
82
    protected function helper(AssetInterface $asset, array $options = []): string
83
    {
84
        $path = $this->str($this->baseUrl) . $this->str($this->basePath) . $this->str($asset->getTargetPath());
85
86
        $extension = pathinfo($path, PATHINFO_EXTENSION);
87
        $extension = strtolower($extension);
0 ignored issues
show
Bug introduced by
It seems like $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

87
        $extension = strtolower(/** @scrutinizer ignore-type */ $extension);
Loading history...
88
89
        if (isset($options['addFileMTime']) && $options['addFileMTime']) {
90
            $path .= '?' . (string) $asset->getLastModified();
91
        }
92
93
        switch ($extension) {
94
            case 'js':
95
                return $this->getScriptTag($path, $options);
96
97
            case 'css':
98
                return $this->getStylesheetTag($path, $options);
99
        }
100
101
        return '';
102
    }
103
104
    private function str(?string $s): string
105
    {
106
        return $s ?? '';
107
    }
108
109
    protected function getScriptTag(string $path, array $options = []): string
110
    {
111
        $type = isset($options['type']) && ! empty($options['type']) ? $options['type'] : 'text/javascript';
112
113
        return '<script type="' . $this->escape($type) . '" src="' . $this->escape($path) . '"></script>';
114
    }
115
116
    protected function getStylesheetTag(string $path, array $options = []): string
117
    {
118
        $media = isset($options['media']) && ! empty($options['media']) ? $options['media'] : 'screen';
119
        $type  = isset($options['type']) && ! empty($options['type']) ? $options['type'] : 'text/css';
120
        $rel   = isset($options['rel']) && ! empty($options['rel']) ? $options['rel'] : 'stylesheet';
121
122
        return '<link href="' . $this->escape($path)
123
            . '" media="' . $this->escape($media)
124
            . '" rel="' . $this->escape($rel)
125
            . '" type="' . $this->escape($type) . '">';
126
    }
127
}
128