Completed
Push — master ( daefc0...7c7725 )
by MusikAnimal
43:00
created

Extension::getIntuitionLang()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
        $intuition->registerDomain('xtools', $path);
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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