Test Failed
Push — master ( da5def...342071 )
by Christian
04:54
created

UpdateOrCreateUser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
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 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