Language   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A getCurrent() 0 12 2
A getLinks() 0 15 2
1
<?php
2
3
namespace Faulancer\View\Helper;
4
5
use Faulancer\Service\Config;
6
use Faulancer\Session\SessionManager;
7
use Faulancer\View\AbstractViewHelper;
8
9
/**
10
 * Class LanguageLink
11
 *
12
 * @package Faulancer\View\Helper
13
 * @author  Florian Knapp <[email protected]>
14
 */
15
class Language extends AbstractViewHelper
16
{
17
18
    /** @var array */
19
    protected $languageTextMapping = [
20
        'de' => 'Deutsch',
21
        'en' => 'English',
22
        'hr' => 'Hrvatski',
23
        'fr' => 'France'
24
    ];
25
26
    /**
27
     * @return Language $this
28
     */
29
    public function __invoke()
30
    {
31
        return $this;
32
    }
33
34
    /**
35
     * @param bool $codeOnly
36
     * @return string
37
     */
38
    public function getCurrent($codeOnly = true)
39
    {
40
        /** @var SessionManager $sessionManager */
41
        $sessionManager = $this->getServiceLocator()->get(SessionManager::class);
42
        $code           = $sessionManager->get('language') ?? 'de';
43
44
        if ($codeOnly) {
45
            return $code;
46
        }
47
48
        return $this->languageTextMapping[$code];
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getLinks()
55
    {
56
        /** @var Config $config */
57
        $config       = $this->getServiceLocator()->get(Config::class);
58
        $translations = $config->get('translation');
59
60
        $result  = [];
61
        $pattern = '<a rel="alternate" hreflang="%s" class="lang %s" href="?lang=%s">%s</a>';
62
63
        foreach ($translations as $key => $content) {
64
            $result[] = sprintf($pattern, strtolower($key), strtolower($key), $key, $this->languageTextMapping[$key]);
65
        }
66
67
        return implode('', $result);
68
    }
69
70
}