Passed
Push — master ( 342071...ce1cac )
by Christian
04:33
created

UpdateOrCreateUser::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 10
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 20
    public function __construct()
27
    {
28 20
        $this->exists = app()->make(ExistsUser::class);
29 20
        $this->create = app()->make(CreateUser::class);
30 20
        $this->update = app()->make(UpdateUser::class);
31 20
        $this->unique = app()->make(UniqueUser::class);
32 20
    }
33
34
    /**
35
     * @param User $user
36
     *
37
     * @throws UserAlreadyExistsException
38
     * @throws UserNameExistsMustBeUniqueException
39
     *
40
     * @return bool
41
     */
42 20
    public function make(User $user): bool
43
    {
44 20
        if (null !== $user->getId()) {
45 10
            return $this->update($user);
46
        }
47
48 20
        return $this->create($user);
49
    }
50
51
    /**
52
     * @param User $user
53
     *
54
     * @throws UserAlreadyExistsException
55
     *
56
     * @return bool
57
     */
58 20
    private function create(User $user): bool
59
    {
60 20
        $exists = $this->exists->make($user);
61
62 20
        if (true === $exists) {
63 1
            throw new UserAlreadyExistsException();
64
        }
65
66 20
        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 10
    private function update(User $user): bool
78
    {
79 10
        $exists = $this->unique->make($user);
80
81 10
        if (true === $exists) {
82 1
            throw new UserNameExistsMustBeUniqueException();
83
        }
84
85 9
        $exists = $this->exists->make($user);
86
87 9
        if (false === $exists) {
88 1
            throw new UserDoesNotExistsException();
89
        }
90
91 8
        return $this->update->make($user);
92
    }
93
}
94