Passed
Branch master (a0cc06)
by Dāvis
17:06
created

DefaultLocaleResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Translatable\Router;
4
5
use JMS\I18nRoutingBundle\Router\LocaleResolverInterface as LocaleResolver;
0 ignored issues
show
Bug introduced by
The type JMS\I18nRoutingBundle\Ro...LocaleResolverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * Default Locale Resolver.
10
 *
11
 * These checks are performed by this method:
12
 *
13
 *     1. Check if the host is associated with a specific locale
14
 *     2. Check for a query parameter named "hl"
15
 *     3. Check for a locale in the session
16
 *     4. Check for a cookie named "hl"
17
 *     5. Check the Accept header for supported languages
18
 *
19
 * @author Johannes M. Schmitt <[email protected]>
20
 */
21
class DefaultLocaleResolver implements LocaleResolver
22
{
23
    private $cookieName;
24
    private $hostMap;
25
26
    public function __construct($cookieName, array $hostMap = [])
27
    {
28
        $this->cookieName = $cookieName;
29
        $this->hostMap = $hostMap;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function resolveLocale(Request $request, array $availableLocales)
36
    {
37
        if ($this->hostMap && isset($this->hostMap[$host = $request->getHost()])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->hostMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            return $this->hostMap[$host];
39
        }
40
41
        // if a locale has been specifically set as a query parameter, use it
42
        if ($request->query->has('hl')) {
43
            $hostLanguage = $request->query->get('hl');
44
45
            if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) {
46
                return $hostLanguage;
47
            }
48
        }
49
50
        // check if a session exists, and if it contains a locale
51
        if ($request->hasPreviousSession()) {
52
            $session = $request->getSession();
53
            if ($session->has('_locale')) {
54
                return $session->get('_locale');
55
            }
56
        }
57
58
        // if user sends a cookie, use it
59
        if ($request->cookies->has($this->cookieName)) {
60
            $hostLanguage = $request->cookies->get($this->cookieName);
61
62
            if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) {
63
                return $hostLanguage;
64
            }
65
        }
66
67
        $languages = [];
68
        foreach ($request->getLanguages() as $language) {
69
            if (strlen($language) != 2) {
70
                $newLang = explode('_', $language, 2);
71
                $languages[] = reset($newLang);
72
            } else {
73
                $languages[] = $language;
74
            }
75
        }
76
        $languages = array_unique($languages);
77
        if (!empty($languages)) {
78
            foreach ($languages as $lang) {
79
                if (in_array($lang, $availableLocales, true)) {
80
                    return $lang;
81
                }
82
            }
83
        }
84
85
        return null;
86
    }
87
}
88