Completed
Push — develop ( b8f7b1...cea6ad )
by
unknown
07:05
created

Locale::factory()   A

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 1
1
<?php
2
/**
3
 * @filesource
4
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
5
 * @license MIT
6
 * @author Miroslav Fedeleš <[email protected]>
7
 * @since 0.26
8
 */
9
10
namespace Core\I18n;
11
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Zend\Http\Request;
14
use Auth\Entity\UserInterface as User;
15
16
class Locale
17
{
18
    
19
    /**
20
     * @var string
21
     */
22
    protected $defaultLanguage;
23
    
24
    /**
25
     * @var array
26
     */
27
    protected $supportedLanguages;
28
    
29
    /**
30
     * @param array $supportedLanguages
31
     */
32
    public function __construct(array $supportedLanguages)
33
    {
34
        $this->supportedLanguages = $supportedLanguages;
35
        $languages = array_keys($supportedLanguages);
36
        $this->defaultLanguage = reset($languages);
0 ignored issues
show
Documentation Bug introduced by
It seems like reset($languages) can also be of type integer or false. However, the property $defaultLanguage is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @param User $user
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user not be null|User?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
     * @return string
43
     */
44
    public function detectLanguage(Request $request, User $user = null)
45
    {
46
        if (isset($user))
47
        {
48
            $settings = $user->getSettings('Core');
49
            
50
            if (isset($settings->localization)
51
                && isset($settings->localization->language)
52
                && $settings->localization->language != '')
53
            {
54
                // return language by user's settings
55
                return $settings->localization->language;
56
            }
57
        }
58
        
59
        $headers = $request->getHeaders();
60
        
61
        if ($headers->has('Accept-Language')) {
62
            $locales = $headers->get('Accept-Language')->getPrioritized();
63
            foreach ($locales as $locale) {
64
                $language = $locale->type;
65
                
66
                if (isset($this->supportedLanguages[$language])) {
67
                    // return language by browser's accept language
68
                    return $language;
69
                }
70
            }
71
        }
72
        
73
        // no match, therefore return default language
74
        return $this->defaultLanguage;
75
    }
76
    
77
    /**
78
     * @param string $lang
79
     * @throws \InvalidArgumentException
80
     * @return string
81
     */
82
    public function getLocaleByLanguage($lang)
83
    {
84
        if (!isset($this->supportedLanguages[$lang]))
85
        {
86
            throw new \InvalidArgumentException(sprintf('Unsupported language: "%s"', $lang));
87
        }
88
        
89
        return $this->supportedLanguages[$lang];
90
    }
91
    
92
    /**
93
     * @param string $lang
94
     * @return boolean
95
     */
96
    public function isLanguageSupported($lang)
97
    {
98
        return isset($this->supportedLanguages[$lang]);
99
    }
100
    
101
    /**
102
	 * @return string
103
	 */
104
	public function getDefaultLanguage()
105
	{
106
		return $this->defaultLanguage;
107
	}
108
}
109