Completed
Push — master ( e56775...ee26f0 )
by Mahmoud
03:40
created

CreateUserByCredentialsTask   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 59
rs 10

2 Methods

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