Issues (6)

src/model/LangSwitcher.php (5 issues)

Labels
1
<?php
2
3
namespace TopviewDigital\LangSwitcher\Model;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class LangSwitcher extends Model
8
{
9
    protected $table = '_lang_switcher';
10
    public $timestamps = false;
11
    /**
12
     * The attributes that are mass assignable.
13
     *
14
     * @var array
15
     */
16
    protected $fillable = [
17
        'class', 'method', 'middleware', 'enable',
18
    ];
19
20
    public static function registerGuard(array $settings)
21
    {
22
        $keys = ['class', 'method', 'middleware', 'enable'];
23
        $settings = array_intersect_key($settings, array_flip($keys));
24
        if (!empty(array_intersect(array_key($settings), $keys))) {
0 ignored issues
show
The function array_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        if (!empty(array_intersect(/** @scrutinizer ignore-call */ array_key($settings), $keys))) {
Loading history...
25
            $enable = $settings['enable'] ?? true;
26
            unset($settings['enable']);
27
            $settings = self::firstOrCreate($settings);
28
            $settings->refresh();
29
            $settings->enable = $enable ? 1 : 0;
30
            $settings->save();
31
        }
32
    }
33
34
    public static function switchLocale($back = false)
35
    {
36
        $switch_to = request()->input(config('lang-switcher.request.input'));
37
        self::setLocale($switch_to);
38
        if ($back) {
39
            return back();
40
        }
41
    }
42
43
    public static function setLocale($switch_to = null)
44
    {
45
        $field = config('lang-switcher.field');
46
        if (config('lang-switcher.via_model')) {
47
            foreach (self::where('enable', 1)->get() as $guard) {
48
                $class = $guard->class;
0 ignored issues
show
The property class does not seem to exist on TopviewDigital\LangSwitcher\Model\LangSwitcher. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
49
                $method = $guard->method;
0 ignored issues
show
The property method does not seem to exist on TopviewDigital\LangSwitcher\Model\LangSwitcher. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
50
                $model = $class::$method();
51
                if ($model && $model->$field) {
52
                    if ($switch_to) {
53
                        $model->$field = $switch_to;
54
                        $model->save();
55
                    }
56
                    App::setLocale($model->$field);
0 ignored issues
show
The type TopviewDigital\LangSwitcher\Model\App was not found. Did you mean App? If so, make sure to prefix the type with \.
Loading history...
57
                }
58
            }
59
        }
60
        if (!request()->session()->has($field) || request()->session()->get($field) != app()->getLocale()) {
0 ignored issues
show
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        if (!request()->session()->has($field) || request()->session()->get($field) != app()->/** @scrutinizer ignore-call */ getLocale()) {
Loading history...
61
            request()->session()->put($field, app()->getLocale());
62
        }
63
    }
64
}
65