Passed
Push — main ( cebaee...66a16c )
by Loïc
04:17
created

RoutingExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Twig\Extension;
6
7
use Symfony\Component\Routing\RouterInterface;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
use function Symfony\Component\String\u;
12
13
/**
14
 * Routing related stuff.
15
 *
16
 * @see RoutingExtensionTest
17
 */
18
final class RoutingExtension extends AbstractExtension
19
{
20
    /**
21
     * @var array<int, string>|null
22
     */
23
    private ?array $controllers = null;
24
25 11
    public function __construct(
26
        private readonly RouterInterface $router,
27
    ) {
28 11
    }
29
30 4
    public function getFunctions(): array
31
    {
32 4
        return [
33 4
            new TwigFunction('ctrl_fqcn', $this->getControllerFqcn(...)),
34 4
            new TwigFunction('attr_if', $this->getAttributeIf(...)),
35 4
            new TwigFunction('aria_current_page_if', $this->getAriaCurrentPageIf(...)),
36 4
        ];
37
    }
38
39
    /**
40
     * This function returns a complete controller FQCN from a short name.
41
     * If you have multiple controllers/actions with the same name, you can include
42
     * the parent namespace to select the good one like the last examples below.
43
     *
44
     * @example ComposerAction      --> App\Controller\ComposerAction
45
     * @example Product\ListAction  --> App\Controller\Product\ListAction
46
     * @example Category\ListAction --> App\Controller\Category\ListAction
47
     *
48
     * @return class-string
49
     */
50 8
    public function getControllerFqcn(string $ctrlShortname): string
51
    {
52 8
        if ($this->controllers === null) {
53 8
            $this->controllers = array_unique(array_map(
54 8
                static fn ($value) => u($value)->trimSuffix('::__invoke')->toString(),
55 8
                array_keys($this->router->getRouteCollection()->getAliases())
56 8
            ));
57
        }
58
59 8
        foreach ($this->controllers as $controller) {
60
            /** @var class-string $controller */
61 8
            if (u($controller)->endsWith($ctrlShortname)) {
62 7
                return $controller;
63
            }
64
        }
65
66
        // If all your ADR controllers live in the "App\Controller\" namespace,
67
        // then you probably don't need all the code above, just use:
68
        //
69
        // return  'App\\Controller\\'. $ctrlShortname;
70
        //
71
        // If the route is not found, then Twig raises a "RouteNotFoundException".
72
73 1
        throw new \InvalidArgumentException('No controller found for the "'.$ctrlShortname.'" shortname.');
74
    }
75
76
    /**
77
     * Returns an HTML attribute with a given value only if a condition is met.
78
     *
79
     * @see templates/base.html.twig
80
     */
81 7
    public function getAttributeIf(bool $condition, string $attribute, string $value): string
82
    {
83 7
        if (!$condition) {
84 7
            return '';
85
        }
86
87 7
        return \sprintf(' %s="%s"', $attribute, $value);
88
    }
89
90 7
    public function getAriaCurrentPageIf(bool $condition): string
91
    {
92 7
        return $this->getAttributeIf($condition, 'aria-current', 'page');
93
    }
94
}
95