Main   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 118
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hookRouteList() 0 9 1
A hookTranslationSetBefore() 0 4 1
A __construct() 0 7 1
B setLanguage() 0 35 6
A getLanguageModel() 0 6 1
1
<?php
2
3
/**
4
 * @package Language detector
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\langdetect;
11
12
use gplcart\core\Container;
13
use gplcart\core\helpers\Server;
14
use gplcart\core\helpers\Session;
15
use gplcart\core\helpers\Url;
16
use gplcart\core\Module;
17
18
/**
19
 * Main class for Language detector module
20
 */
21
class Main
22
{
23
24
    /**
25
     * URL helper class instance
26
     * @var \gplcart\core\helpers\Url $url
27
     */
28
    protected $url;
29
30
    /**
31
     * Module class instance
32
     * @var \gplcart\core\Module $module
33
     */
34
    protected $module;
35
36
    /**
37
     * Session helper class instance
38
     * @var \gplcart\core\helpers\Session $session
39
     */
40
    protected $session;
41
42
    /**
43
     * Server helper class instance
44
     * @var \gplcart\core\helpers\Server $server
45
     */
46
    protected $server;
47
48
    /**
49
     * Main constructor.
50
     * @param Module $module
51
     * @param Url $url
52
     * @param Server $server
53
     * @param Session $session
54
     */
55
    public function __construct(Module $module, Url $url, Server $server, Session $session)
56
    {
57
        $this->url = $url;
58
        $this->module = $module;
59
        $this->server = $server;
60
        $this->session = $session;
61
    }
62
63
    /**
64
     * Implements hook "route.list"
65
     * @param array $routes
66
     */
67
    public function hookRouteList(array &$routes)
68
    {
69
        $routes['admin/module/settings/langdetect'] = array(
70
            'access' => 'module_edit',
71
            'handlers' => array(
72
                'controller' => array('gplcart\\modules\\langdetect\\controllers\\Settings', 'editSettings')
73
            )
74
        );
75
    }
76
77
    /**
78
     * Implements hook "translation.set.before"
79
     * @param string $langcode
80
     */
81
    public function hookTranslationSetBefore($langcode)
82
    {
83
        $this->setLanguage($langcode);
84
    }
85
86
    /**
87
     * Sets detected language
88
     * @param $langcode
89
     * @return null
90
     */
91
    protected function setLanguage($langcode)
92
    {
93
        $settings = $this->module->getSettings('langdetect');
94
95
        if (empty($settings['redirect'])) {
96
            return null;
97
        }
98
99
        $saved = $this->session->get('langdetect');
100
101
        if (isset($saved)) {
102
            return null;
103
        }
104
105
        $detected_langcode = $this->server->httpLanguage();
106
107
        if (!in_array($detected_langcode, $settings['redirect'])) {
108
            return null;
109
        }
110
111
        $language = $this->getLanguageModel()->get($detected_langcode);
112
113
        if (empty($language['status'])) {
114
            return null;
115
        }
116
117
        $this->session->set('langdetect', $detected_langcode);
118
119
        if ($detected_langcode !== $langcode) {
120
            $redirect = $this->url->language($detected_langcode, $this->url->path());
121
            $this->url->redirect($redirect, array(), true, true);
122
        }
123
124
        return null;
125
    }
126
127
    /**
128
     * Language model class instance
129
     * @return \gplcart\core\models\Language
130
     */
131
    protected function getLanguageModel()
132
    {
133
        /** @var \gplcart\core\models\Language $instance */
134
        $instance = Container::get('gplcart\\core\\models\\Language');
135
        return $instance;
136
    }
137
138
}
139