Passed
Push — dev ( c1a5f7...902aab )
by Enjoys
07:50
created

AssetsExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
eloc 11
dl 0
loc 61
ccs 12
cts 14
cp 0.8571
rs 10
c 5
b 1
f 3
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFunctions() 0 6 1
A asset() 0 3 1
A getExternCss() 0 3 1
A getAssetsCollector() 0 3 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 Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
/**
12
 * Class AssetsExtension
13
 * Set assets from Twig. In the css example, but for js the same
14
 * {{  asset('css', [{0: '//cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css', 'minify': false}]) }}
15
 * {{  asset('css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css') }}
16
 * {{  asset('css', ['path/style1.css', 'style2.css']) }}
17
 *
18
 * Output
19
 * {{ eCSS() }}
20
 * {{ eJS() }}
21
 *
22
 * @package Enjoys\AssetsCollector\Extensions\Twig
23
 */
24
class AssetsExtension extends AbstractExtension
25
{
26
    /**
27
     * @var Assets
28
     */
29
    private Assets $assetsCollector;
30
31 4
    public function __construct(Assets $assetsCollector)
32
    {
33 4
        $this->assetsCollector = $assetsCollector;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function getFunctions(): array
40
    {
41
        return [
42 1
            new TwigFunction('asset', [$this, 'asset']),
43 1
            new TwigFunction('eCSS', [$this, 'getExternCss'], ['is_safe' => ['html']]),
44 1
            new TwigFunction('eJS', [$this, 'getExternJs'], ['is_safe' => ['html']]),
45
        ];
46
    }
47
48
    /**
49
     * @param string $type
50
     * @param array<string> $paths
51
     * @param string $namespace
52
     * @param string $method
53
     */
54 4
    public function asset(string $type, array $paths = [], string $namespace = Assets::NAMESPACE_COMMON, string $method = 'push'): void
55
    {
56 4
        $this->assetsCollector->add($type, $paths, $namespace, $method);
57
    }
58
59
    /**
60
     * @param string $namespace
61
     * @return string
62
     * @throws \Exception
63
     */
64 1
    public function getExternCss(string $namespace = Assets::NAMESPACE_COMMON): string
65
    {
66 1
        return $this->assetsCollector->get('css', $namespace);
67
    }
68
69
    /**
70
     * @param string $namespace
71
     * @return string
72
     * @throws \Exception
73
     */
74 1
    public function getExternJs(string $namespace = Assets::NAMESPACE_COMMON): string
75
    {
76 1
        return $this->assetsCollector->get('js', $namespace);
77
    }
78
79
    /**
80
     * @return Assets
81
     */
82
    public function getAssetsCollector(): Assets
83
    {
84
        return $this->assetsCollector;
85
    }
86
}
87