1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Domains\Permissions\Features; |
4
|
|
|
|
5
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionAlreadyExistsException; |
6
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionDoesNotExistsException; |
7
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionNameExistsMustBeUniqueException; |
8
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Jobs\CreatePermission; |
9
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Jobs\ExistsPermission; |
10
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Jobs\UniquePermission; |
11
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Jobs\UpdatePermission; |
12
|
|
|
use Omatech\Mage\Core\Domains\Permissions\Permission; |
13
|
|
|
|
14
|
|
|
class UpdateOrCreatePermission |
15
|
|
|
{ |
16
|
|
|
private $exists; |
17
|
|
|
private $create; |
18
|
|
|
private $update; |
19
|
|
|
private $unique; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* UpdateOrCreatePermission constructor. |
23
|
|
|
* |
24
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
25
|
|
|
*/ |
26
|
8 |
|
public function __construct() |
27
|
|
|
{ |
28
|
8 |
|
$this->exists = app()->make(ExistsPermission::class); |
29
|
8 |
|
$this->create = app()->make(CreatePermission::class); |
30
|
8 |
|
$this->update = app()->make(UpdatePermission::class); |
31
|
8 |
|
$this->unique = app()->make(UniquePermission::class); |
32
|
8 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Permission $permission |
36
|
|
|
* |
37
|
|
|
* @throws PermissionAlreadyExistsException |
38
|
|
|
* @throws PermissionNameExistsMustBeUniqueException |
39
|
|
|
* @throws PermissionDoesNotExistsException |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
8 |
|
public function make(Permission $permission): bool |
44
|
|
|
{ |
45
|
8 |
|
if (null !== $permission->getId()) { |
46
|
|
|
return $this->update($permission); |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
return $this->create($permission); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Permission $permission |
54
|
|
|
* |
55
|
|
|
* @throws PermissionAlreadyExistsException |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
8 |
|
private function create(Permission $permission): bool |
60
|
|
|
{ |
61
|
8 |
|
$exists = $this->exists->make($permission); |
62
|
|
|
|
63
|
|
|
if (true === $exists) { |
64
|
|
|
throw new PermissionAlreadyExistsException(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->create->make($permission); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Permission $permission |
72
|
|
|
* |
73
|
|
|
* @throws PermissionNameExistsMustBeUniqueException |
74
|
|
|
* @throws PermissionDoesNotExistsException |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
private function update(Permission $permission): bool |
79
|
|
|
{ |
80
|
|
|
$exists = $this->unique->make($permission); |
81
|
|
|
|
82
|
|
|
if (true === $exists) { |
83
|
|
|
throw new PermissionNameExistsMustBeUniqueException(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$exists = $this->exists->make($permission); |
87
|
|
|
|
88
|
|
|
if (false === $exists) { |
89
|
|
|
throw new PermissionDoesNotExistsException(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->update->make($permission); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|