Completed
Push — master ( 2ed8e9...bac0b2 )
by Dev
07:05
created

PageCanonicalService::getLocale()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use Twig\Extension\AbstractExtension;
6
use Twig\TwigFilter;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use PiedWeb\RenderAttributes\AttributesTrait;
9
use Twig\TwigFunction;
10
11
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12
use Symfony\Component\HttpKernel\HttpKernel;
13
use Symfony\Component\HttpKernel\HttpKernelInterface;
14
use Symfony\Component\Routing\Router;
15
16
class PageCanonicalService
17
{
18
    protected $request;
19
    protected $router;
20
    protected $defaultLocaleWithoutPrefix;
21
    protected $defaultLocale;
22
    protected $locale;
23
24
    public function __construct(
25
        RequestStack $request,
26
        Router $router,
27
        bool $defaultLocaleWithoutPrefix = false,
28
        ?string $defaultLocale = null
29
    )
30
    {
31
        $this->request = $request->getCurrentRequest();
32
        $this->router = $router;
33
        $this->defaultLocaleWithoutPrefix = $defaultLocaleWithoutPrefix;
34
        $this->defaultLocale = $defaultLocale;
35
    }
36
37
    public function generatePathForHomepage(?string $expectedLocale = null)
38
    {
39
        if (false === $this->isForDefaultLocale($expectedLocale)) {
40
            return $this->router->generate('localized_piedweb_cms_page', [
41
                'slug'=> '',
42
                '_locale' => $this->getLocale($expectedLocale)
43
            ]);
44
        }
45
46
        return $this->router->generate('piedweb_cms_homepage');
47
    }
48
49
    /**
50
     * @var string $expectedLocale Permit to generate a link for another language than the current request
51
     */
52
    public function generatePathForPage(string $slug, ?string $expectedLocale = null)
53
    {
54
        if ($slug == '') {
55
            return $this->generatePathForHomepage($expectedLocale);
56
        }
57
58
        if (
59
            $this->defaultLocale === null
60
            || ($this->isForDefaultLocale($expectedLocale) && $this->defaultLocaleWithoutPrefix)
61
        ) {
62
            return $this->router->generate('piedweb_cms_page', ['slug'=>$slug]);
63
        }
64
65
        return $this->router->generate('localized_piedweb_cms_page', [
66
            'slug'=>$slug,
67
            '_locale' => $this->getLocale($expectedLocale)
68
        ]);
69
    }
70
71
    protected function isForDefaultLocale(?string $expectedLocale = null)
72
    {
73
        return null === $this->defaultLocale // maybe it's not an i18n app
74
            || ($expectedLocale !== null && $this->defaultLocale == $expectedLocale)
75
            || ($expectedLocale === null && $this->request !== null && $this->defaultLocale == $this->request->getLocale())
76
        ;
77
    }
78
79
    /**
80
     * Always get a locale even if we are not in a request
81
     */
82
    protected function getLocale(?string $expectedLocale)
83
    {
84
        return $expectedLocale !== null ? $expectedLocale : ($this->request === null ? $this->defaultLocale : $this->request->getLocale());
85
    }
86
}
87