1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Dashboard |
5
|
|
|
* @author Ian Olson <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2015, Laraflock |
8
|
|
|
* @link https://github.com/laraflock |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Laraflock\Dashboard\Repositories\Permission; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\QueryException; |
14
|
|
|
use Laraflock\Dashboard\Exceptions\PermissionsException; |
15
|
|
|
use Laraflock\Dashboard\Models\Permission; |
16
|
|
|
use Laraflock\Dashboard\Repositories\Base\BaseRepository; |
17
|
|
|
|
18
|
|
|
class PermissionRepository extends BaseRepository implements PermissionRepositoryInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Permission instance. |
22
|
|
|
* |
23
|
|
|
* @var \Laraflock\Dashboard\Models\Permission |
24
|
|
|
*/ |
25
|
|
|
protected $permission; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The constructor. |
29
|
|
|
* |
30
|
|
|
* @param \Laraflock\Dashboard\Models\Permission $permission |
31
|
|
|
*/ |
32
|
110 |
|
public function __construct(Permission $permission) |
33
|
|
|
{ |
34
|
110 |
|
$this->permission = $permission; |
35
|
110 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
4 |
|
public function getAll() |
41
|
|
|
{ |
42
|
4 |
|
return $this->permission->all(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
14 |
|
public function getById($id) |
49
|
|
|
{ |
50
|
14 |
|
return $this->permission->find($id); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
24 |
|
public function create(array $data, $validate = true) |
57
|
|
|
{ |
58
|
24 |
|
$this->rules = [ |
59
|
24 |
|
'name' => 'required', |
60
|
24 |
|
'slug' => 'required|unique:permissions', |
61
|
|
|
]; |
62
|
|
|
|
63
|
24 |
|
if ($validate) { |
64
|
13 |
|
$this->validate($data); |
65
|
12 |
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
24 |
|
$permission = $this->permission->create($data); |
69
|
24 |
|
} catch (QueryException $e) { |
70
|
1 |
|
throw new PermissionsException(trans('dashboard::dashboard.errors.permission.create')); |
71
|
|
|
} |
72
|
|
|
|
73
|
24 |
|
return $permission; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
6 |
|
public function update(array $data, $id, $validate = true) |
80
|
|
|
{ |
81
|
6 |
|
if (!$permission = $this->getById($id)) { |
82
|
2 |
|
throw new PermissionsException(trans('dashboard::dashboard.errors.permission.found')); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$this->rules = [ |
86
|
4 |
|
'name' => 'required', |
87
|
4 |
|
'slug' => 'required|alpha_dash', |
88
|
|
|
]; |
89
|
|
|
|
90
|
4 |
|
if ($permission->slug != $data['slug']) { |
91
|
3 |
|
$this->rules['slug'] = 'required|alpha_dash|unique:permissions'; |
92
|
3 |
|
} |
93
|
|
|
|
94
|
4 |
|
if ($validate) { |
95
|
3 |
|
$this->validate($data); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
2 |
|
$permission->name = $data['name']; |
99
|
2 |
|
$permission->slug = $data['slug']; |
100
|
2 |
|
$permission->save(); |
101
|
|
|
|
102
|
2 |
|
return $permission; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
4 |
|
public function delete($id) |
109
|
|
|
{ |
110
|
4 |
|
if (!$permission = $this->getById($id)) { |
111
|
2 |
|
throw new PermissionsException(trans('dashboard::dashboard.errors.permission.found')); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$permission->delete(); |
115
|
|
|
|
116
|
2 |
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: