SetLocaleMiddleware::handle()   C
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 0 Features 9
Metric Value
c 11
b 0
f 9
dl 0
loc 34
rs 6.7272
cc 7
eloc 25
nc 24
nop 2
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Support\Facades\Auth;
7
8
class SetLocaleMiddleware
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param \Illuminate\Http\Request $request
14
     * @param \Closure                 $next
15
     *
16
     * @return mixed
17
     */
18
    public function handle($request, Closure $next)
0 ignored issues
show
Coding Style introduced by
handle uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
19
    {
20
        if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
21
            $http_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
22
        }else{
23
            $http_lang = 'en';
24
        }
25
26
        switch ($http_lang) {
27
            case 'de':
28
                $userLangs = 'de';
29
                break;
30
            case 'en':
31
                $userLangs = 'en';
32
                break;
33
            case 'es':
34
                $userLangs = 'es';
35
                break;
36
            default:
37
                $userLangs = 'en';
38
        }
39
40
        if (Auth::check()) {
41
            if (Auth::user()->settings->language == '') {
0 ignored issues
show
Bug introduced by
Accessing settings 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...
42
                \App::setLocale($userLangs);
43
            } else {
44
                \App::setLocale(\Auth::user()->settings->language);
45
            }
46
        } else {
47
            \App::setLocale($userLangs);
48
        }
49
50
        return $next($request);
51
    }
52
}
53