AssetsExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 6
Bugs 1 Features 3
Metric Value
eloc 26
c 6
b 1
f 3
dl 0
loc 77
ccs 27
cts 28
cp 0.9643
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A asset() 0 27 4
A getExternCss() 0 3 1
A __construct() 0 4 1
A getFunctions() 0 6 1
A getExternJs() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector\Extensions\Twig;
6
7
use Enjoys\AssetsCollector\Assets;
8
use Enjoys\AssetsCollector\AssetType;
9
use Twig\Error\LoaderError;
10
use Twig\Extension\AbstractExtension;
11
use Twig\Loader\LoaderInterface;
12
use Twig\TwigFunction;
13
14
/**
15
 * Class AssetsExtension
16
 * Set assets from Twig. In the css example, but for js the same
17
 * {{  asset('css', [{0: '//cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css', 'minify': false}]) }}
18
 * {{  asset('css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css') }}
19
 * {{  asset('css', ['path/style1.css', 'style2.css']) }}
20
 *
21
 * Output
22
 * {{ eCSS() }}
23
 * {{ eJS() }}
24
 *
25
 * @package Enjoys\AssetsCollector\Extensions\Twig
26
 */
27
class AssetsExtension extends AbstractExtension
28
{
29
    /**
30
     * @var Assets
31
     */
32
    private Assets $assetsCollector;
33
    private ?LoaderInterface $loader;
34 6
35
    public function __construct(Assets $assetsCollector, ?LoaderInterface $loader = null)
36 6
    {
37 6
        $this->assetsCollector = $assetsCollector;
38
        $this->loader = $loader;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43 1
     */
44
    public function getFunctions(): array
45 1
    {
46 1
        return [
47 1
            new TwigFunction('asset', [$this, 'asset']),
48 1
            new TwigFunction('eCSS', [$this, 'getExternCss'], ['is_safe' => ['html']]),
49 1
            new TwigFunction('eJS', [$this, 'getExternJs'], ['is_safe' => ['html']]),
50
        ];
51
    }
52
53
    /**
54
     * @throws LoaderError
55 6
     */
56
    public function asset(
57
        string|AssetType $type,
58
        array $paths = [],
59
        string $group = Assets::GROUP_COMMON,
60
        string $method = 'push'
61 6
    ): void {
62 6
63 6
        $type = AssetType::tryToAssetType($type);
64 6
        if ($type === null) {
65 6
            return;
66
        }
67
68 1
        $this->assetsCollector->add(
69 1
            $type,
70 1
            array_map(function ($item) {
71
                if ($this->loader === null) {
72 1
                    return $item;
73 6
                }
74 6
                /** @var string[] $path */
75 6
                $path = (array)$item;
76 6
                if ($this->loader->exists($path[0])) {
77
                    $path[0] = $this->loader->getSourceContext($path[0])->getPath();
78
                }
79
                return $path;
80
            }, $paths),
81
            $group,
82
            $method
83
        );
84 3
    }
85
86 3
    /**
87
     * @param string $group
88
     * @return string
89
     * @throws \Exception
90
     */
91
    public function getExternCss(string $group = Assets::GROUP_COMMON): string
92
    {
93
        return $this->assetsCollector->get(AssetType::CSS, $group);
94 1
    }
95
96 1
    /**
97
     * @param string $group
98
     * @return string
99
     * @throws \Exception
100
     */
101
    public function getExternJs(string $group = Assets::GROUP_COMMON): string
102
    {
103
        return $this->assetsCollector->get(AssetType::JS, $group);
104
    }
105
106
}
107