HasPreferences::preferences()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace NukaCode\Users\Traits;
4
5
class HasPreferences
6
{
7
    public function preferences()
8
    {
9
        return $this->belongsToMany('NukaCode\Users\Models\User\Preference', 'preferences_users', 'user_id', 'preference_id')
0 ignored issues
show
Bug introduced by
The method belongsToMany() does not seem to exist on object<NukaCode\Users\Traits\HasPreferences>.

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...
10
                    ->withPivot('value')
11
                    ->orderBy('id', 'asc');
12
    }
13
14
    public function getVisiblePreferences()
15
    {
16
        return Preference::where('hiddenFlag', 0)->orderByNameAsc()->get();
17
    }
18
19
    public function updatePreferenceByKeyName($preferenceKeyName, $preferenceValue)
20
    {
21
        $preference = $this->getPreferenceByKeyName($preferenceKeyName);
22
23
        $this->setPreferenceValue($preference->id, $preferenceValue);
24
    }
25
26
    public function getPreferenceValueByKeyName($preferenceKeyName)
27
    {
28
        $preference = Preference::where('keyName', $preferenceKeyName)->first();
29
30
        if ($preference != null) {
31
            $userPreference = UserPreferenceUser::where('preference_id', $preference->id)->where('user_id', $this->id)->first();
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
            if ($userPreference == null) {
34
                return $preference->default;
35
            }
36
37
            return $userPreference->value;
38
        }
39
    }
40
41
    public function getPreferenceByKeyName($preferenceKeyName)
42
    {
43
        $preference = Preference::where('keyName', $preferenceKeyName)->first();
44
45
        if ($preference != null) {
46
            $userPreference = PreferenceUser::where('preference_id', $preference->id)->where('user_id', $this->id)->first();
47
48
            if ($userPreference == null) {
49
                $userPreference                = new PreferenceUser();
50
                $userPreference->user_id       = $this->id;
51
                $userPreference->preference_id = $preference->id;
52
                $userPreference->value         = $preference->default;
53
                $userPreference->save();
54
            }
55
56
            return $userPreference;
57
        }
58
59
        return null;
60
    }
61
62
    public function getPreferenceById($preferenceId)
63
    {
64
        return UserPreferenceUser::find($preferenceId);
65
    }
66
67
    public function getPreferenceValue($keyName)
68
    {
69
        $preference = $this->getPreferenceByKeyName($keyName);
70
71
        return $preference->value;
72
    }
73
74
    public function getPreferenceOptionsArray($id)
75
    {
76
        $preference = $this->getPreferenceById($id);
77
78
        $preferenceOptions = explode('|', $preference->preference->value);
79
        $preferenceArray   = [];
80
81
        foreach ($preferenceOptions as $preferenceOption) {
82
            $preferenceArray[$preferenceOption] = ucwords($preferenceOption);
83
        }
84
85
        return $preferenceArray;
86
    }
87
88
    public function setPreferenceValue($id, $value)
89
    {
90
        $preference = $this->getPreferenceById($id);
91
92
        if ($value != $preference->value) {
93
            $preference->value = $value;
94
95
            if (! $preference->save()) {
96
                throw new \Exception($preference->getErrors());
97
            }
98
        }
99
    }
100
101
    public function resetPreferenceToDefault($id)
102
    {
103
        $preference = $this->getPreferenceById($id);
104
105
        $preference->value = $preference->preference->default;
106
        $preference->save();
107
108
        return $this;
109
    }
110
}