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

Asset::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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