Extensions   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 54
ccs 2
cts 12
cp 0.1666
rs 10
c 2
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getClasses() 0 3 1
A suggestFunction() 0 9 4
A suggestFilter() 0 9 4
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-extra-bundle
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\TwigExtraBundle;
13
14
use Ocubom\Twig\Extension\HtmlExtension;
15
use Ocubom\Twig\Extension\SvgExtension;
16
use Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension;
17
use Twig\Error\SyntaxError;
18
19
final class Extensions
20
{
21
    private const EXTENSIONS = [
22
        'html' => [
23
            'name' => 'html',
24
            'class' => HtmlExtension::class,
25
            'class_name' => 'OcubomHtmlExtension',
26
            'package' => 'ocubom/twig-html-extension',
27
            'filters' => ['html_attributes', 'html_compress'],
28
            'functions' => [],
29
        ],
30
        'svg' => [
31
            'name' => 'svg',
32
            'class' => SvgExtension::class,
33
            'class_name' => 'OcubomSvgExtension',
34
            'package' => 'ocubom/twig-svg-extension',
35
            'filters' => ['svg', 'svg_symbols', 'fontawesome', 'iconify'],
36
            'functions' => ['fa', 'svg'],
37
        ],
38
        'webpack_encore' => [
39
            'name' => 'webpack_encore',
40
            'class' => EntryFilesTwigExtension::class, // Use the official twig extension
41
            'class_name' => 'OcubomWebpackEncoreExtension',
42
            'package' => 'symfony/webpack-encore-bundle', // Needed to enable this
43
            'filters' => [],
44
            'functions' => ['encore_entry_css_source', 'encore_entry_js_source'],
45
        ],
46
    ];
47
48 14
    public static function getClasses(): array
49
    {
50 14
        return array_column(self::EXTENSIONS, 'class', 'name');
51
    }
52
53
    public static function suggestFilter(string $name): bool
54
    {
55
        foreach (self::EXTENSIONS as $extension) {
56
            if (isset($extension['filters']) && in_array($name, $extension['filters'])) {
57
                throw new SyntaxError(sprintf('The "%s" filter is part of the %s, which is not installed/enabled; try running "composer require %s".', $name, $extension['class_name'], $extension['package']));
58
            }
59
        }
60
61
        return false;
62
    }
63
64
    public static function suggestFunction(string $name): bool
65
    {
66
        foreach (self::EXTENSIONS as $extension) {
67
            if (isset($extension['functions']) && in_array($name, $extension['functions'])) {
68
                throw new SyntaxError(sprintf('The "%s" function is part of the %s, which is not installed/enabled; try running "composer require %s".', $name, $extension['class_name'], $extension['package']));
69
            }
70
        }
71
72
        return false;
73
    }
74
}
75