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