LangSwitcher::render()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.2448
c 0
b 0
f 0
cc 7
nc 12
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2014 Oleksandr Torosh (http://wezoom.net)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
7
namespace Application\Mvc\Helper;
8
9
use Cms\Model\Language;
10
use Phalcon\Mvc\User\Component;
11
12
class LangSwitcher extends Component
13
{
14
15
    public function render($lang, $string)
16
    {
17
        $router = $this->getDI()->get('router');
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...
18
        $view = $this->getDI()->get('view');
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...
19
        $url = $this->getDI()->get('url');
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...
20
        $requestQuery = new RequestQuery();
21
22
        $matched_route = $router->getMatchedRoute();
23
24
        if ($matched_route) {
25
            $route_name = $matched_route->getName();
26
            $route_name_changed = $this->changeRouteName($route_name, $lang);
27
28
            $route_exitsts = $router->getRouteByName($route_name_changed);
29
            if ($route_exitsts) {
30
                $url_args = array();
31
                $url_args['for'] = $route_name_changed;
32
33
                $route_params = $router->getParams();
34
                if ($route_params) {
35
                    foreach ($route_params as $param_key => $param_val) {
36
                        $url_args[$param_key] = $param_val;
37
                    }
38
                }
39
40
                $href = $url->get($url_args);
41
            } else {
42
                $uri = $router->getRewriteUri();
43
                $href = $uri . $requestQuery->getSymbol() . '?lang=' . $lang;
44
            }
45
        } else {
46
            $uri = $router->getRewriteUri();
47
            $href = $uri . $requestQuery->getSymbol() . '?lang=' . $lang;
48
        }
49
50
        if ($lang == LANG) {
51
            $html = '<span>' . $string . '</span>';
52
        } elseif ($view->disabledLang == $lang) {
53
            $html = '<span class="disabled">' . $string . '</span>';
54
        } else {
55
            $html = '<a href="' . $href . '">' . $string . '</a>';
56
        }
57
58
        return $html;
59
60
    }
61
62
    private function changeRouteName($route_name, $lang)
63
    {
64
        $iso_array = Language::findCachedLanguagesIso();
65
        if (!empty($iso_array)) {
66
            foreach ($iso_array as $iso) {
67
                $route_name = str_replace('_' . $iso, '', $route_name);
68
            }
69
        }
70
        return $route_name . '_' . $lang;
71
    }
72
73
} 
74