forked from matthusby/maintenance
/
Repository.php
264 lines (236 loc) · 6.05 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace Stevebauman\Maintenance\Repositories;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
use Stevebauman\Maintenance\Models\Revision;
use Cartalyst\DataGrid\Laravel\Facades\DataGrid;
abstract class Repository
{
/**
* Returns the current model instance.
*
* @return Model
*/
abstract public function model();
/**
* Returns all of the models results.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all()
{
return $this->model()->all();
}
/**
* Finds the specified record by its ID.
*
* @param int|string $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return null|\Illuminate\Database\Eloquent\Model
*/
public function find($id)
{
return $this->model()->findOrFail($id);
}
/**
* Finds the specified archived record by its ID.
*
* @param int|string $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return mixed
*/
public function findArchived($id)
{
return $this->model()->onlyTrashed()->findOrFail($id);
}
/**
* Deletes a record on the current model.
*
* @param int|string $id
*
* @return bool|null
*
* @throws \Exception
*/
public function delete($id)
{
return $this->model()->destroy($id);
}
/**
* Deletes an archived record on the current model.
*
* @param int|string $id
*
* @return bool
*/
public function deleteArchived($id)
{
$record = $this->findArchived($id);
if($record) {
$record->forceDelete();
return true;
}
return false;
}
/**
* Restores an archived record.
*
* @param int|string $id
*
* @return bool
*/
public function restore($id)
{
$record = $this->findArchived($id);
if($record) {
return $record->restore();
}
return false;
}
/**
* Returns a new grid instance of the current model.
*
* @param array $columns
* @param array $settings
* @param \Closure $transformer
*
* @return \Cartalyst\DataGrid\DataGrid
*/
public function grid(array $columns = [], array $settings = [], $transformer = null)
{
$model = $this->model();
return $this->newGrid($model, $columns, $settings, $transformer);
}
/**
* Returns a new grid instance of the current models archived records.
*
* @param array $columns
* @param array $settings
* @param \Closure $transformer
*
* @return \Cartalyst\DataGrid\DataGrid
*/
public function gridArchived(array $columns = [], array $settings = [], $transformer = null)
{
$model = $this->model()->onlyTrashed();
return $this->newGrid($model, $columns, $settings, $transformer);
}
/**
* Constructs a new data grid instance
* tailored to revision history settings.
*
* @param mixed $data
*
* @return \Cartalyst\DataGrid\DataGrid
*/
public function newHistoryGrid($data)
{
$columns = [
'id',
'user_id',
'revisionable_type',
'revisionable_id',
'key',
'new_value',
'old_value',
'created_at',
];
$settings = [
'sort' => 'created_at',
'direction' => 'desc',
'threshold' => 10,
'throttle' => 11,
];
$transformer = function(Revision $revision)
{
return [
'user' => $revision->getUserResponsible()->full_name,
'key' => $revision->getKey(),
'old_value' => (! is_null($revision->getOldValue()) ? $revision->getOldValue() : '<em>None</em>'),
'new_value' => ($revision->getNewValue() ? $revision->getNewValue() : '<em>None</em>'),
'created_at' => $revision->created_at->format('Y-m-d g:i a'),
];
};
return $this->newGrid($data, $columns, $settings, $transformer);
}
/**
* Constructs a new data grid instance with the
* specified resource, columns and settings.
*
* @param mixed $data
* @param array $columns
* @param array $settings
* @param \Closure $transformer
*
* @return \Cartalyst\DataGrid\DataGrid
*/
public function newGrid($data, array $columns = [], array $settings = [], $transformer = null)
{
return DataGrid::make($data, $columns, $settings, $transformer);
}
/**
* Caches an item with the specified key and value.
*
* @param int|string $key
* @param int $minutes
* @param \Closure $closure
*
* @return mixed
*/
public function cache($key, $minutes = 60, \Closure $closure)
{
return Cache::remember($key, $minutes, $closure);
}
/**
* Caches an item with the
* specified key and value forever.
*
* @param int|string $key
* @param \Closure $closure
*
* @return mixed
*/
public function cacheForever($key, $closure)
{
return Cache::rememberForever($key, $closure);
}
/**
* Returns true / false if the cache
* contains an item with the specified key.
*
* @param int|string $key
*
* @return bool
*/
public function cacheHas($key)
{
return Cache::has($key);
}
/**
* Returns true / false if the key
* specified has been forgotten in the cache.
*
* @param int|string $key
*
* @return bool
*/
public function cacheForget($key)
{
return Cache::forget($key);
}
/**
* Converts a string to a date for inserting into the database.
*
* @param string $str
*
* @return bool|string
*/
protected function strToDate($str)
{
return date('Y-m-d H:i:s', strtotime($str));
}
}