LocalizerController::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Aitor24\Localizer\Controllers;
4
5
use Aitor24\Localizer\Facades\LocalizerFacade as Localizer;
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
10
class LocalizerController extends Controller
11
{
12
    /**
13
     * Set locale if it's allowed.
14
     *
15
     * @param string                   $locale
16
     * @param \Illuminate\Http\Request $request
17
     **/
18
    private function setLocale($locale, $request)
19
    {
20
        $allowedLangs = array_keys(Localizer::allowedLanguages());
21
        if (config('localizer.block_unallowed_langs') && !in_array($locale, $allowedLangs)) {
22
            abort(404, 'Unallowed language');
23
        }
24
25
        if (Auth::check()) {
26
            $user = Auth::User();
27
            $user->locale = $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...
28
            $user->save();
29
        } else {
30
            $request->session()->put('locale', $locale);
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
        }
32
    }
33
34
    /**
35
     * Set locale and return home url.
36
     *
37
     * @param string                   $locale
38
     * @param \Illuminate\Http\Request $request
39
     **/
40
    public function setHome($locale, Request $request)
41
    {
42
        $this->setLocale($locale, $request);
43
44
        return redirect(url('/'));
0 ignored issues
show
Bug introduced by
It seems like url('/') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, redirect() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
    }
46
47
    /**
48
     * Set locale and return back.
49
     *
50
     * @param string                   $locale
51
     * @param \Illuminate\Http\Request $request
52
     **/
53
    public function set($locale, Request $request)
54
    {
55
        $this->setLocale($locale, $request);
56
57
        return redirect()->back();
58
    }
59
}
60