CreateUserByCredentialsTask   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B run() 0 24 3
1
<?php
2
3
namespace App\Containers\User\Tasks;
4
5
use App\Containers\Authentication\Tasks\ApiLoginThisUserObjectTask;
6
use App\Containers\User\Contracts\UserRepositoryInterface;
7
use App\Containers\User\Exceptions\AccountFailedException;
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
    /**
21
     * @var \App\Containers\User\Contracts\UserRepositoryInterface
22
     */
23
    private $userRepository;
24
25
    /**
26
     * @var \App\Containers\Authentication\Tasks\ApiLoginThisUserObjectTask
27
     */
28
    private $apiLoginThisUserObjectTask;
29
30
    /**
31
     * CreateUserByCredentialsTask constructor.
32
     *
33
     * @param \App\Containers\User\Contracts\UserRepositoryInterface        $userRepository
34
     * @param \App\Containers\Authentication\Tasks\ApiLoginThisUserObjectTask $apiLoginThisUserObjectTask
35
     */
36
    public function __construct(
37
        UserRepositoryInterface $userRepository,
38
        ApiLoginThisUserObjectTask $apiLoginThisUserObjectTask
39
    ) {
40
        $this->userRepository = $userRepository;
41
        $this->apiLoginThisUserObjectTask = $apiLoginThisUserObjectTask;
42
    }
43
44
    /**
45
     * @param      $email
46
     * @param      $password
47
     * @param      $name
48
     * @param null $gender
49
     * @param null $birth
50
     * @param bool $login
51
     *
52
     * @return  mixed
53
     */
54
    public function run($email, $password, $name, $gender = null, $birth = null, $login = false)
55
    {
56
        $hashedPassword = Hash::make($password);
57
58
        try {
59
            // create new user
60
            $user = $this->userRepository->create([
61
                'name'     => $name,
62
                'email'    => $email,
63
                'password' => $hashedPassword,
64
                'gender'   => $gender,
65
                'birth'    => $birth,
66
            ]);
67
        } catch (Exception $e) {
68
            throw (new AccountFailedException())->debug($e);
69
        }
70
71
        if ($login) {
72
            // login this user using it's object and inject it's token on it
73
            $user = $this->apiLoginThisUserObjectTask->run($user);
74
        }
75
76
        return $user;
77
    }
78
79
}
80