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

UpdateOrCreateUser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 56.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 78
ccs 13
cts 23
cp 0.5652
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A make() 0 7 2
A update() 0 15 3
A create() 0 9 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
     * @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