Localize::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Middleware;
13
14
use Closure;
15
use Illuminate\Config\Repository;
16
use Jenssegers\Date\Date;
17
18
class Localize
19
{
20
    /**
21
     * Array of languages Gitamin can use.
22
     *
23
     * @var array
24
     */
25
    protected $langs;
26
27
    /**
28
     * Config repository.
29
     *
30
     * @var \Illuminate\Config\Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * Constructs a new localize instance.
36
     *
37
     * @param \Illuminate\Config\Repository $config
38
     */
39
    public function __construct(Repository $config)
40
    {
41
        $this->config = $config;
42
        $this->langs = $config->get('langs');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('langs') of type * is incompatible with the declared type array of property $langs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    /**
46
     * Handle an incoming request.
47
     *
48
     * @param \Illuminate\Http\Request $request
49
     * @param \Closure                 $next
50
     *
51
     * @return mixed
52
     */
53
    public function handle($request, Closure $next)
54
    {
55
        $supportedLanguages = $request->getLanguages();
56
        $userLanguage = $this->config->get('app.locale');
57
58 View Code Duplication
        foreach ($supportedLanguages as $language) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $language = str_replace('_', '-', $language);
60
61
            if (isset($this->langs[$language])) {
62
                $userLanguage = $language;
63
                break;
64
            }
65
        }
66
67
        app('translator')->setLocale($userLanguage);
68
        Date::setLocale($userLanguage);
69
70
        return $next($request);
71
    }
72
}
73