1
|
|
|
<?php namespace Anomaly\PreferencesModule\Preference; |
2
|
|
|
|
3
|
|
|
use Anomaly\PreferencesModule\Preference\Contract\PreferenceInterface; |
4
|
|
|
use Anomaly\PreferencesModule\Preference\Contract\PreferenceRepositoryInterface; |
5
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter; |
6
|
|
|
use Anomaly\Streams\Platform\Entry\EntryRepository; |
7
|
|
|
use Anomaly\UsersModule\User\Contract\UserInterface; |
8
|
|
|
use Illuminate\Contracts\Auth\Guard; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PreferenceRepositoryInterface |
12
|
|
|
* |
13
|
|
|
* @link http://pyrocms.com/ |
14
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
15
|
|
|
* @author Ryan Thompson <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PreferenceRepository extends EntryRepository implements PreferenceRepositoryInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The authentication guard. |
22
|
|
|
* |
23
|
|
|
* @var Guard |
24
|
|
|
*/ |
25
|
|
|
protected $auth; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The preference model. |
29
|
|
|
* |
30
|
|
|
* @var PreferenceModel |
31
|
|
|
*/ |
32
|
|
|
protected $model; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The preferences collection. |
36
|
|
|
* |
37
|
|
|
* @var PreferenceCollection |
38
|
|
|
*/ |
39
|
|
|
protected $preferences; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new PreferenceRepositoryInterface instance. |
43
|
|
|
* |
44
|
|
|
* @param Guard $auth |
45
|
|
|
* @param PreferenceModel $model |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Guard $auth, PreferenceModel $model) |
48
|
|
|
{ |
49
|
|
|
$this->auth = $auth; |
50
|
|
|
$this->model = $model; |
51
|
|
|
|
52
|
|
|
$this->preferences = new PreferenceCollection(); |
53
|
|
|
|
54
|
|
|
if ($user = $this->auth->user()) { |
|
|
|
|
55
|
|
|
$this->preferences = $this->model->belongingToUser($auth->getUser())->get(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return if a preference exists or not. |
61
|
|
|
* |
62
|
|
|
* @param $key |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function has($key) |
66
|
|
|
{ |
67
|
|
|
return $this->preferences->has($key); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get a preference. |
72
|
|
|
* |
73
|
|
|
* @param $key |
74
|
|
|
* @return null|PreferenceInterface |
75
|
|
|
*/ |
76
|
|
|
public function get($key) |
77
|
|
|
{ |
78
|
|
|
return $this->preferences->get($key); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set a preferences value. |
83
|
|
|
* |
84
|
|
|
* @param $key |
85
|
|
|
* @param $value |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function set($key, $value) |
89
|
|
|
{ |
90
|
|
|
if (!$user = $this->auth->getUser()) { |
91
|
|
|
throw new \Exception('The user could not be determined.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$preference = $this->findByKeyOrNew($key); |
95
|
|
|
|
96
|
|
|
$preference->setUser($user); |
97
|
|
|
$preference->setValue($value); |
98
|
|
|
|
99
|
|
|
return $this->save($preference); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get a preference value presenter instance. |
104
|
|
|
* |
105
|
|
|
* @param $key |
106
|
|
|
* @param null $default |
107
|
|
|
* @return FieldTypePresenter|null |
108
|
|
|
*/ |
109
|
|
|
public function value($key, $default = null) |
110
|
|
|
{ |
111
|
|
|
if ($preference = $this->get($key)) { |
112
|
|
|
return $preference->getValue(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $default; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Return the field type |
120
|
|
|
* presenter for a preference. |
121
|
|
|
* |
122
|
|
|
* @param $key |
123
|
|
|
* @return FieldTypePresenter|null |
124
|
|
|
*/ |
125
|
|
|
public function presenter($key) |
126
|
|
|
{ |
127
|
|
|
if ($preference = $this->get($key)) { |
128
|
|
|
return $preference->getFieldTypePresenter('value'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Find a preference by it's key |
136
|
|
|
* or return a new instance. |
137
|
|
|
* |
138
|
|
|
* @param $key |
139
|
|
|
* @return PreferenceInterface |
140
|
|
|
*/ |
141
|
|
|
public function findByKeyOrNew($key) |
142
|
|
|
{ |
143
|
|
|
/* @var UserInterface $user */ |
144
|
|
|
if (!$user = $this->auth->getUser()) { |
145
|
|
|
throw new \Exception('The user could not be determined.'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!$preference = $this->model->where('key', $key)->where('user_id', $user->getId())->first()) { |
149
|
|
|
|
150
|
|
|
$preference = $this->model->newInstance(); |
151
|
|
|
|
152
|
|
|
$preference->setKey($key); |
153
|
|
|
$preference->setUser($user); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $preference; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Find all preferences with namespace. |
161
|
|
|
* |
162
|
|
|
* @param $namespace |
163
|
|
|
* @return PreferenceCollection |
164
|
|
|
*/ |
165
|
|
|
public function findAllByNamespace($namespace) |
166
|
|
|
{ |
167
|
|
|
return $this->model->where('key', 'LIKE', $namespace . '%')->get(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.