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