PreferenceModel::scopeBelongingToUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php namespace Anomaly\PreferencesModule\Preference;
2
3
use Anomaly\PreferencesModule\Preference\Command\GetValueFieldType;
4
use Anomaly\PreferencesModule\Preference\Command\GetValuePresenter;
5
use Anomaly\PreferencesModule\Preference\Command\ModifyValue;
6
use Anomaly\PreferencesModule\Preference\Contract\PreferenceInterface;
7
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
8
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
9
use Anomaly\Streams\Platform\Model\Preferences\PreferencesPreferencesEntryModel;
10
use Anomaly\UsersModule\User\Contract\UserInterface;
11
use Illuminate\Database\Eloquent\Builder;
12
13
/**
14
 * Class PreferenceModel
15
 *
16
 * @method Builder belongingToUser(UserInterface $user)
17
 *
18
 * @link          http://pyrocms.com/
19
 * @author        PyroCMS, Inc. <[email protected]>
20
 * @author        Ryan Thompson <[email protected]>
21
 */
22
class PreferenceModel extends PreferencesPreferencesEntryModel implements PreferenceInterface
23
{
24
25
    /**
26
     * Return the value field.
27
     *
28
     * @return FieldType
29
     */
30
    public function field()
31
    {
32
        /* @var FieldType $field */
33
        $field = $this->dispatch(new GetValueFieldType($this));
34
35
        if (!$field) {
36
            return null;
37
        }
38
39
        return $field;
40
    }
41
42
    /**
43
     * Limit to preferences belonging to the provided user.
44
     *
45
     * @param Builder $query
46
     * @param UserInterface $user
47
     */
48
    public function scopeBelongingToUser(Builder $query, UserInterface $user)
49
    {
50
        $query->where('user_id', $user->getId());
51
    }
52
53
    /**
54
     * Get the key.
55
     *
56
     * @return string
57
     */
58
    public function getKey()
59
    {
60
        return $this->key;
61
    }
62
63
    /**
64
     * Set the key.
65
     *
66
     * @param $key
67
     * @return $this
68
     */
69
    public function setKey($key)
70
    {
71
        $this->key = $key;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Get the user.
78
     *
79
     * @return UserInterface
80
     */
81
    public function getUser()
82
    {
83
        return $this->user;
84
    }
85
86
    /**
87
     * Set the user.
88
     *
89
     * @param  UserInterface $user
90
     * @return $this
91
     */
92
    public function setUser(UserInterface $user)
93
    {
94
        $this->user = $user;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Get the value.
101
     *
102
     * @return mixed
103
     */
104
    public function getValue()
105
    {
106
        return $this->value;
107
    }
108
109
    /**
110
     * Set the value.
111
     *
112
     * @param $value
113
     * @return $this
114
     */
115
    public function setValue($value)
116
    {
117
        $this->value = $value;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Set the value.
124
     *
125
     * @param $value
126
     * @return $this
127
     */
128
    protected function setValueAttribute($value)
129
    {
130
        $this->attributes['value'] = $this->dispatch(new ModifyValue($this, $value));
131
132
        return $this;
133
    }
134
135
    /**
136
     * Get the value attribute.
137
     *
138
     * @return mixed
139
     */
140
    protected function getValueAttribute()
141
    {
142
        if (!$field = $this->field()) {
143
            return null;
144
        }
145
146
        return $field->getValue();
147
    }
148
149
    /**
150
     * Get the field type's presenter
151
     * for a given field slug.
152
     *
153
     * We're overriding this to catch
154
     * the "value" key.
155
     *
156
     * @param $fieldSlug
157
     * @return FieldTypePresenter
158
     */
159
    public function getFieldTypePresenter($fieldSlug)
160
    {
161
        if ($fieldSlug == 'value') {
162
            return $this->dispatch(new GetValuePresenter($this));
163
        }
164
165
        return parent::getFieldTypePresenter($fieldSlug);
166
    }
167
}
168