Localization::__construct()   B
last analyzed

Complexity

Conditions 7
Paths 48

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.3946
c 0
b 0
f 0
cc 7
nc 48
nop 1
1
<?php
2
3
/**
4
 * Localization
5
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
6
 * @author Aleksandr Torosh <[email protected]>
7
 */
8
9
namespace YonaCMS\Plugin;
10
11
use Application\Mvc\Helper\CmsCache;
12
use Phalcon\Mvc\User\Plugin;
13
use Phalcon\Mvc\Dispatcher;
14
15
class Localization extends Plugin
16
{
17
18
    public function __construct(Dispatcher $dispatcher)
19
    {
20
        $cmsCache = new CmsCache();
21
        $languages = $cmsCache->get('languages');
22
23
        $defaultLangArray = array_values(array_slice($languages, 0, 1));
24
        $defaultLang = $defaultLangArray[0];
25
26
        $request = $this->getDI()->get('request');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
27
        $queryLang = $request->getQuery('lang');
28
        if (!$queryLang) {
29
            $langParam = $dispatcher->getParam('lang');
30
        } else {
31
            $langParam = $queryLang;
32
        }
33
34
        if (!$langParam) {
35
            $langParam = $defaultLang['iso'];
36
        }
37
38
        foreach ($languages as $language) {
39
            if ($langParam == $language['iso']) {
40
                define('LANG', $language['iso']);
41
                define('LANG_URL', '/' . $language['url']);
42
            }
43
        }
44
        if (!defined('LANG')) {
45
            define('LANG', $defaultLang['iso']);
46
            \Application\Mvc\Model\Model::$lang = $defaultLang['iso'];
47
        }
48
        if (!defined('LANG_URL')) {
49
            define('LANG_URL', $defaultLang['url']);
50
        }
51
52
        $translations = \Cms\Model\Translate::findCachedByLangInArray(LANG);
53
        $this->getDI()->set('translate', new \Phalcon\Translate\Adapter\NativeArray(['content' => $translations]));
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
54
    }
55
}