UpdateOrCreateUser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 73
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A make() 0 8 2
A create() 0 10 2
A update() 0 16 3
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