Language::setLanguage()   C
last analyzed

Complexity

Conditions 11
Paths 15

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 22
nc 15
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\I18n;
3
4
use Cake\Controller\Controller;
5
use Cake\Core\Configure;
6
use Cake\I18n\I18n;
7
8
class Language
9
{
10
11
    /**
12
     * The Cookie instance.
13
     *
14
     * @var \Cake\Network\Session
15
     */
16
    protected $_session;
17
18
    /**
19
     * The Cookie instance.
20
     *
21
     * @var \Cake\Controller\Component\CookieComponent
22
     */
23
    protected $_cookie;
24
25
    /**
26
     * The Controller instance.
27
     *
28
     * @var \Cake\Controller\Controller
29
     */
30
    protected $_controller;
31
32
    /**
33
     * All locales allowed to be used.
34
     *
35
     * @var array
36
     */
37
    protected $_locales = [];
38
39
    /**
40
     * The locale to be used.
41
     *
42
     * @var string
43
     */
44
    protected $_locale;
45
46
    /**
47
     * Construct method.
48
     *
49
     * @param \Cake\Controller\Controller $controller The controller that was fired.
50
     */
51
    public function __construct(Controller $controller)
52
    {
53
        $this->_controller = $controller;
54
        $this->_session = $controller->request->session();
55
        $this->_cookie = $controller->Cookie;
56
        $this->_locales = Configure::read('I18n.locales');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Cake\Core\Configure::read('I18n.locales') of type * is incompatible with the declared type array of property $_locales.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->_locale = I18n::locale();
58
59
        $this->_cookie->configKey('language', [
60
            'expires' => '+1 year',
61
            'httpOnly' => true
62
        ]);
63
    }
64
65
    /**
66
     * Set the language for the user.
67
     *
68
     * @return void
69
     */
70
    public function setLanguage()
71
    {
72
        if ($this->_controller->Auth->user()) {
73
            //The user has already a valid language defined in the database.
74
            if ($this->_session->read('Auth.User.language') && isset($this->_locales[$this->_session->read('Auth.User.language')])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_session->read('Auth.User.language') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
75
                //If the user has not the cookie, we set the cookie.
76
                if (!$this->_cookie->check('language') || $this->_cookie->read('language') != $this->_session->read('Auth.User.language')) {
77
                    $this->_cookie->write('language', $this->_session->read('Auth.User.language'));
78
                }
79
                //Stock the locale of the user.
80
                $this->_locale = $this->_session->read('Auth.User.language');
81
            }
82
        } else {
83
            //The user has a valid cookie.
84
            if ($this->_cookie->check('language') && isset($this->_locales[$this->_cookie->read('language')])) {
85
                $this->_locale = $this->_cookie->read('language');
86
            }
87
        }
88
89
        //The user want to change his language.
90
        if (!is_null($this->_controller->request->getParam('lang')) && isset($this->_locales[$this->_controller->request->getParam('lang')])) {
91
            //If the user is connected, we need to save the new language in the database and refresh his session.
92
            if ($this->_controller->Auth->user()) {
93
                $this->_controller->loadModel('Users');
94
95
                $user = $this->_controller->Users
96
                    ->find()
97
                    ->where(['id' => $this->_session->read('Auth.User.id')])
98
                    ->first();
99
100
                $user->language = $this->_controller->request->getParam('lang');
101
                $this->_controller->Users->save($user);
102
                $this->_session->write('Auth.User.language', $this->_controller->request->getParam('lang'));
103
            }
104
105
            //Save the new language in the cookie.
106
            $this->_cookie->write('language', $this->_controller->request->getParam('lang'));
107
108
            $this->_locale = $this->_controller->request->getParam('lang');
109
        }
110
111
        //Set the locale.
112
        I18n::locale($this->_locale);
113
    }
114
}
115