|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Shop\Roles\Repositories; |
|
3
|
|
|
|
|
4
|
|
|
use App\Shop\Base\BaseRepository; |
|
5
|
|
|
use App\Shop\Permissions\Permission; |
|
6
|
|
|
use App\Shop\Roles\Exceptions\CreateRoleErrorException; |
|
7
|
|
|
use App\Shop\Roles\Exceptions\DeleteRoleErrorException; |
|
8
|
|
|
use App\Shop\Roles\Exceptions\RoleNotFoundErrorException; |
|
9
|
|
|
use App\Shop\Roles\Exceptions\UpdateRoleErrorException; |
|
10
|
|
|
use App\Shop\Roles\Role; |
|
11
|
|
|
use Illuminate\Database\QueryException; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
|
|
14
|
|
|
class RoleRepository extends BaseRepository implements RoleRepositoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Role |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $model; |
|
20
|
|
|
/** |
|
21
|
|
|
* RoleRepository constructor. |
|
22
|
|
|
* @param Role $role |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Role $role) |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct($role); |
|
27
|
|
|
$this->model = $role; |
|
28
|
|
|
} |
|
29
|
|
|
/** |
|
30
|
|
|
* List all Roles |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $order |
|
33
|
|
|
* @param string $sort |
|
34
|
|
|
* @return Collection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function listRoles(string $order = 'id', string $sort = 'desc') : Collection |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->all(['*'], $order, $sort); |
|
39
|
|
|
} |
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $data |
|
42
|
|
|
* @return Role |
|
43
|
|
|
* @throws CreateRoleErrorException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function createRole(array $data) : Role |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
|
|
$role = new Role($data); |
|
49
|
|
|
$role->save(); |
|
50
|
|
|
return $role; |
|
51
|
|
|
} catch (QueryException $e) { |
|
52
|
|
|
throw new CreateRoleErrorException($e); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param int $id |
|
58
|
|
|
* |
|
59
|
|
|
* @return Role |
|
60
|
|
|
* @throws RoleNotFoundErrorException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function findRoleById(int $id) : Role |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
66
|
|
|
} catch (QueryException $e) { |
|
67
|
|
|
throw new RoleNotFoundErrorException($e); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $data |
|
73
|
|
|
* @param int $id |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
* @throws UpdateRoleErrorException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function updateRole(array $data, int $id) : bool |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
return $this->update($data, $id); |
|
82
|
|
|
} catch (QueryException $e) { |
|
83
|
|
|
throw new UpdateRoleErrorException($e); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param int $id |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
* @throws DeleteRoleErrorException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function deleteRoleById(int $id) : bool |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
return $this->delete($id); |
|
97
|
|
|
} catch (QueryException $e) { |
|
98
|
|
|
throw new DeleteRoleErrorException($e); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Permission $permission |
|
104
|
|
|
*/ |
|
105
|
|
|
public function attachToPermission(Permission $permission) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->model->attachPermission($permission); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param Permission ...$permissions |
|
112
|
|
|
*/ |
|
113
|
|
|
public function attachToPermissions(... $permissions) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->model->attachPermissions($permissions); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param array $ids |
|
120
|
|
|
*/ |
|
121
|
|
|
public function syncPermissions(array $ids) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->model->syncPermissions($ids); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return Collection |
|
128
|
|
|
*/ |
|
129
|
|
|
public function listPermissions() : Collection |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->model->permissions()->get(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|