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
|
|
|
* Find a preference by it's key |
121
|
|
|
* or return a new instance. |
122
|
|
|
* |
123
|
|
|
* @param $key |
124
|
|
|
* @return PreferenceInterface |
125
|
|
|
*/ |
126
|
|
|
public function findByKeyOrNew($key) |
127
|
|
|
{ |
128
|
|
|
/* @var UserInterface $user */ |
129
|
|
|
if (!$user = $this->auth->getUser()) { |
130
|
|
|
throw new \Exception('The user could not be determined.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!$preference = $this->model->where('key', $key)->where('user_id', $user->getId())->first()) { |
134
|
|
|
|
135
|
|
|
$preference = $this->model->newInstance(); |
136
|
|
|
|
137
|
|
|
$preference->setKey($key); |
138
|
|
|
$preference->setUser($user); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $preference; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Find all preferences with namespace. |
146
|
|
|
* |
147
|
|
|
* @param $namespace |
148
|
|
|
* @return PreferenceCollection |
149
|
|
|
*/ |
150
|
|
|
public function findAllByNamespace($namespace) |
151
|
|
|
{ |
152
|
|
|
return $this->model->where('key', 'LIKE', $namespace . '%')->get(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.