UrlGeneratorExtension::generateSiteUrl()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\Twig;
6
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
/**
12
 * @author Joris van de Sande <[email protected]>
13
 */
14
final class UrlGeneratorExtension extends AbstractExtension
15
{
16
    /**
17
     * @var UrlGeneratorInterface
18
     */
19
    private $urlGenerator;
20
21
    public function __construct(UrlGeneratorInterface $urlGenerator)
22
    {
23
        $this->urlGenerator = $urlGenerator;
24
    }
25
26
    public function getFunctions(): array
27
    {
28
        return [
29
            new TwigFunction('site_path', [$this, 'generateSitePath']),
30
            new TwigFunction('site_url', [$this, 'generateSiteUrl']),
31
        ];
32
    }
33
34
    public function generateSitePath(string $name, array $parameters = []): string
35
    {
36
        return $this->urlGenerator->generate($name, $parameters);
37
    }
38
39
    public function generateSiteUrl($site, string $name, array $parameters = [], $schemeRelative = false): string
40
    {
41
        $parameters['site'] = $site;
42
43
        return $this->urlGenerator->generate(
44
            $name,
45
            $parameters,
46
            $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL
47
        );
48
    }
49
}
50