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
|
21 |
|
public function __construct() |
25
|
|
|
{ |
26
|
21 |
|
$this->exists = new ExistsRole(); |
27
|
21 |
|
$this->create = new CreateRole(); |
28
|
21 |
|
$this->update = new UpdateRole(); |
29
|
21 |
|
$this->unique = new UniqueRole(); |
30
|
21 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Role $role |
34
|
|
|
* @return bool |
35
|
|
|
* @throws RoleAlreadyExistsException |
36
|
|
|
* @throws RoleDoesNotExistsException |
37
|
|
|
* @throws RoleNameExistsMustBeUniqueException |
38
|
|
|
*/ |
39
|
21 |
|
public function make(Role $role): bool |
40
|
|
|
{ |
41
|
21 |
|
if (null !== $role->getId()) { |
42
|
6 |
|
return $this->update($role); |
43
|
|
|
} |
44
|
|
|
|
45
|
21 |
|
return $this->create($role); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Role $role |
50
|
|
|
* @return bool |
51
|
|
|
* @throws RoleAlreadyExistsException |
52
|
|
|
*/ |
53
|
21 |
|
private function create(Role $role): bool |
54
|
|
|
{ |
55
|
21 |
|
$exists = $this->exists->make($role); |
56
|
|
|
|
57
|
21 |
|
if (true === $exists) { |
58
|
1 |
|
throw new RoleAlreadyExistsException(); |
59
|
|
|
} |
60
|
|
|
|
61
|
21 |
|
return $this->create->make($role); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Role $role |
66
|
|
|
* @return bool |
67
|
|
|
* @throws RoleDoesNotExistsException |
68
|
|
|
* @throws RoleNameExistsMustBeUniqueException |
69
|
|
|
*/ |
70
|
6 |
|
private function update(Role $role): bool |
71
|
|
|
{ |
72
|
6 |
|
$exists = $this->unique->make($role); |
73
|
|
|
|
74
|
6 |
|
if (true === $exists) { |
75
|
1 |
|
throw new RoleNameExistsMustBeUniqueException(); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
$exists = $this->exists->make($role); |
79
|
|
|
|
80
|
5 |
|
if (false === $exists) { |
81
|
1 |
|
throw new RoleDoesNotExistsException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return $this->update->make($role); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|