LocaleResolver::getAvailableLocales()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Victoire\Bundle\I18nBundle\Resolver;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * A class to guess the locale form URL
9
 * ref: victoire_i18n.locale_resolver.
10
 */
11
class LocaleResolver
12
{
13
    const PATTERN_PARAMETER = 'parameter'; // pass the locale the normal way, ie. http://acme.dn/fr
14
    const PATTERN_DOMAIN = 'domain';
15
16
    public $localePattern;
17
    protected $localeDomainConfig;
18
    protected $availableLocales;
19
    public $defaultLocale;
20
    public $defaultDomain;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param string $localePattern      What is the strategy to resolve locale
26
     * @param array  $localeDomainConfig The locale domain config
27
     * @param string $defaultLocale      The default local app
28
     * @param string $availableLocales   The list of available locales
29
     */
30
    public function __construct($localePattern, array $localeDomainConfig, $defaultLocale, $availableLocales)
31
    {
32
        $this->localePattern = $localePattern;
33
        $this->localeDomainConfig = $localeDomainConfig;
34
        $this->defaultLocale = $defaultLocale;
35
        $this->availableLocales = $availableLocales;
36
37
        foreach ($this->localeDomainConfig as $_domain => $_locale) {
38
            if ($_locale == $this->defaultLocale) {
39
                $this->defaultDomain = $_domain;
40
                break;
41
            }
42
        }
43
    }
44
45
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
46
     * set the local depending on patterns
47
     * it also set the victoire_locale wich is the locale of the application admin.
48
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
49
    public function resolve(Request $request)
50
    {
51
        //locale
52
        switch ($this->localePattern) {
53
            case self::PATTERN_DOMAIN:
54
                $locale = $this->resolveFromDomain($request);
55
                $request->setLocale($locale);
56
                break;
57
        }
58
59
        return $request->getLocale();
60
    }
61
62
    /**
63
     * @param Request $request
64
     *
65
     * @throws \Exception
66
     *
67
     * @return string
68
     *
69
     * resolves the locale from httpHost or host
70
     */
71
    public function resolveFromDomain(Request $request)
72
    {
73
        $host = $request->getHost();
74
        $httpHost = $request->getHttpHost();
75
76
        if (array_key_exists($host, $this->localeDomainConfig)) {
77
            return $this->localeDomainConfig[$host];
78
        } elseif (array_key_exists($httpHost, $this->localeDomainConfig)) {
79
            return $this->localeDomainConfig[$httpHost];
80
        }
81
82
        error_log(sprintf(
83
            'Host "%s" is not defined in your locale_pattern_table in app/config/victoire_core.yml (%s available), using default locale (%s) instead',
84
            $httpHost,
85
            implode(',', $this->localeDomainConfig),
86
            $this->defaultLocale
87
        ));
88
89
        return $request->getLocale();
90
    }
91
92
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$locale" missing
Loading history...
93
     * @return string
94
     *
95
     * This method resolves the domain from locale
96
     */
97
    public function resolveDomainForLocale($locale)
98
    {
99
        foreach ($this->localeDomainConfig as $domain => $domainLocale) {
100
            if ($locale === $domainLocale) {
101
                return $domain;
102
            }
103
        }
104
105
        return $this->defaultLocale;
106
    }
107
108
    /**
109
     * Return available locales.
110
     *
111
     * @return array
112
     */
113
    public function getAvailableLocales()
114
    {
115
        return $this->availableLocales;
116
    }
117
118
    /**
119
     * return domain config.
120
     *
121
     * @return array
122
     */
123
    public function getDomainConfig()
124
    {
125
        return $this->localeDomainConfig;
126
    }
127
}
128