LocalizerMiddleware::setIfAllowed()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace Aitor24\Localizer\Middlewares;
4
5
use Aitor24\Localizer\Facades\LocalizerFacade as Localizer;
6
use Carbon\Carbon;
7
use Closure;
8
use Illuminate\Support\Facades\App;
9
use Illuminate\Support\Facades\Auth;
10
use Unicodeveloper\Identify\Facades\IdentityFacade as Identify;
11
12
class LocalizerMiddleware
13
{
14
    /**
15
     * This function checks if language to set is an allowed lang of config.
16
     *
17
     * @param string $lang
18
     **/
19
    private function setIfAllowed($lang)
20
    {
21
        $allowedLangs = array_keys(Localizer::allowedLanguages());
22
        if (config('localizer.block_unallowed_langs') && !in_array($lang, $allowedLangs)) {
23
            $lang = $allowedLangs[0];
24
        }
25
26
        App::setLocale($lang);
27
        if (config('localizer.carbon')) {
28
            Carbon::setLocale($lang);
29
        }
30
    }
31
32
    /**
33
     * This function checks if user is logged in, and loads the prefered language.
34
     *
35
     * @param string $lang
0 ignored issues
show
Bug introduced by
There is no parameter named $lang. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     **/
37
    public function setLang($request)
38
    {
39
        if (Auth::check()) {
40
            $user = Auth::user();
41
42
            // Set user locale
43
            if ($user->locale) {
0 ignored issues
show
Bug introduced by
Accessing locale on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
44
                $this->setIfAllowed($user->locale);
0 ignored issues
show
Bug introduced by
Accessing locale on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
45 View Code Duplication
            } else {
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...
46
                if (config('localizer.set_auto_lang')) {
47
                    $this->setIfAllowed(Identify::lang()->getLanguage());
48
                } else {
49
                    $this->setIfAllowed(config('localizer.default_lang'));
50
                }
51
            }
52
        } else {
53
            if ($request->session()->has('locale')) {
54
                $this->setIfAllowed(session('locale'));
55 View Code Duplication
            } else {
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...
56
                if (config('localizer.set_auto_lang')) {
57
                    $this->setIfAllowed(Identify::lang()->getLanguage());
58
                } else {
59
                    $this->setIfAllowed(config('localizer.default_lang'));
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * Handle an incoming request.
67
     *
68
     * @param \Illuminate\Http\Request $request
69
     * @param \Closure                 $next
70
     *
71
     * @return mixed
72
     */
73
    public function handle($request, Closure $next)
74
    {
75
        $this->setLang($request);
76
77
        return $next($request);
78
    }
79
}
80