UpdateOrCreateUser::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 19
    public function __construct()
25
    {
26 19
        $this->exists = new ExistsUser();
27 19
        $this->create = new CreateUser();
28 19
        $this->update = new UpdateUser();
29 19
        $this->unique = new UniqueUser();
30 19
    }
31
32
    /**
33
     * @param User $user
34
     * @return bool
35
     * @throws UserAlreadyExistsException
36
     * @throws UserDoesNotExistsException
37
     * @throws UserNameExistsMustBeUniqueException
38
     */
39 19
    public function make(User $user): bool
40
    {
41 19
        if (null !== $user->getId()) {
42 10
            return $this->update($user);
43
        }
44
45 19
        return $this->create($user);
46
    }
47
48
    /**
49
     * @param User $user
50
     * @return bool
51
     * @throws UserAlreadyExistsException
52
     */
53 19
    private function create(User $user): bool
54
    {
55 19
        $exists = $this->exists->make($user);
56
57 19
        if (true === $exists) {
58 1
            throw new UserAlreadyExistsException();
59
        }
60
61 19
        return $this->create->make($user);
62
    }
63
64
    /**
65
     * @param User $user
66
     * @return bool
67
     * @throws UserDoesNotExistsException
68
     * @throws UserNameExistsMustBeUniqueException
69
     */
70 10
    private function update(User $user): bool
71
    {
72 10
        $exists = $this->unique->make($user);
73
74 10
        if (true === $exists) {
75 1
            throw new UserNameExistsMustBeUniqueException();
76
        }
77
78 9
        $exists = $this->exists->make($user);
79
80 9
        if (false === $exists) {
81 1
            throw new UserDoesNotExistsException();
82
        }
83
84 8
        return $this->update->make($user);
85
    }
86
}
87