Passed
Pull Request — master (#125)
by MusikAnimal
03:53
created

Extension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the Extension class.
4
 */
5
6
namespace AppBundle\Twig;
7
8
use Symfony\Component\Config\Definition\Exception\Exception;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
use Intuition;
13
use Symfony\Component\HttpFoundation\Session\Session;
14
use Twig_Extension;
15
16
/**
17
 * The parent class for all of XTools' Twig extensions, in order to centralize the i18n set-up.
18
 */
19
abstract class Extension extends Twig_Extension
20
{
21
22
    /** @var ContainerInterface The DI container. */
23
    protected $container;
24
25
    /** @var RequestStack The request stack. */
26
    protected $requestStack;
27
28
    /** @var Session User's current session. */
29
    protected $session;
30
31
    /** @var Intuition The i18n object. */
32
    private $intuition;
33
34
    /**
35
     * Extension constructor.
36
     * @param ContainerInterface $container The DI container.
37
     * @param RequestStack $requestStack The request stack.
38
     * @param Session $session
39
     */
40 22
    public function __construct(ContainerInterface $container, RequestStack $requestStack, Session $session)
41
    {
42 22
        $this->container = $container;
43 22
        $this->requestStack = $requestStack;
44 22
        $this->session = $session;
45 22
    }
46
47
    /**
48
     * Get an Intuition object, set to the current language based on the query string or session
49
     * of the current request.
50
     * @return Intuition
51
     * @throws \Exception If the 'i18n/en.json' file doesn't exist (as it's the default).
52
     */
53 17
    protected function getIntuition()
54
    {
55
        // Don't recreate the object.
56 17
        if ($this->intuition instanceof Intuition) {
57 12
            return $this->intuition;
58
        }
59
60
        // Find the path, and complain if English doesn't exist.
61 17
        $path = $this->container->getParameter('kernel.root_dir') . '/../i18n';
62 17
        if (!file_exists("$path/en.json")) {
63
            throw new Exception("Language directory doesn't exist: $path");
64
        }
65
66 17
        $useLang = 'en';
67
68
        // Current request doesn't exist in unit tests, in which case we'll fall back to English.
69 17
        if ($this->requestStack->getCurrentRequest() !== null) {
70 11
            $useLang = $this->getIntuitionLang();
71
72
            // Save the language to the session.
73 11
            if ($this->session->get('lang') !== $useLang) {
74 11
                $this->session->set('lang', $useLang);
75
            }
76
        }
77
78
        // Set up Intuition, using the selected language.
79 17
        $intuition = new Intuition('xtools');
80 17
        $intuition->registerDomain('xtools', $path);
0 ignored issues
show
Bug introduced by
$path of type string is incompatible with the type array expected by parameter $dir of Intuition::registerDomain(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $intuition->registerDomain('xtools', /** @scrutinizer ignore-type */ $path);
Loading history...
81 17
        $intuition->setLang(strtolower($useLang));
82
83 17
        $this->intuition = $intuition;
84 17
        return $intuition;
85
    }
86
87
    /**
88
     * Shorthand to get the current request from the request stack.
89
     * @return Request
90
     * There is no request stack in the tests.
91
     * @codeCoverageIgnore
92
     */
93
    protected function getCurrentRequest()
94
    {
95
        return $this->container->get('request_stack')->getCurrentRequest();
96
    }
97
98
    /**
99
     * Determine the interface language, either from the current request or session.
100
     * @return string
101
     */
102 11
    private function getIntuitionLang()
103
    {
104 11
        $queryLang = $this->requestStack->getCurrentRequest()->query->get('uselang');
105 11
        $sessionLang = $this->session->get('lang');
106
107 11
        if ($queryLang !== '' && $queryLang !== null) {
108 1
            return $queryLang;
109 11
        } elseif ($sessionLang !== '' && $sessionLang !== null) {
110
            return $sessionLang;
111
        }
112
113
        // English as default.
114 11
        return 'en';
115
    }
116
}
117