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