Completed
Push — master ( d86698...3d0fab )
by
unknown
03:08
created

CreateUserByCredentialsTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\User\Tasks;
4
5
use App\Containers\User\Data\Repositories\UserRepository;
6
use App\Containers\User\Models\User;
7
use App\Ship\Exceptions\CreateResourceFailedException;
8
use App\Ship\Parents\Tasks\Task;
9
use Exception;
10
use Illuminate\Support\Facades\Hash;
11
12
/**
13
 * Class CreateUserByCredentialsTask.
14
 *
15
 * @author Mahmoud Zalt <[email protected]>
16
 */
17
class CreateUserByCredentialsTask extends Task
18
{
19
20
    protected $repository;
21
22
    public function __construct(UserRepository $repository)
23
    {
24
        $this->repository = $repository;
25
    }
26
27
    /**
28
     * @param bool        $isClient
29
     * @param string      $email
30
     * @param string      $password
31
     * @param string|null $name
32
     * @param string|null $gender
33
     * @param string|null $birth
34
     *
35
     * @return  mixed
36
     * @throws  CreateResourceFailedException
37
     */
38
    public function run(
39
        bool $isClient = true,
40
        string $email,
41
        string $password,
42
        string $name = null,
43
        string $gender = null,
44
        string $birth = null
45
    ): User {
46
47
        try {
48
            // create new user
49
            $user = $this->repository->create([
50
                'password'  => Hash::make($password),
51
                'email'     => $email,
52
                'name'      => $name,
53
                'gender'    => $gender,
54
                'birth'     => $birth,
55
                'is_client' => $isClient,
56
            ]);
57
58
        } catch (Exception $e) {
59
            throw (new CreateResourceFailedException())->debug($e);
60
        }
61
62
        return $user;
63
    }
64
65
}
66