Completed
Push — master ( 012634...6d13cc )
by Mahmoud
03:45
created

CreateUserService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A byCredentials() 0 22 3
A byAgent() 0 15 2
1
<?php
2
3
namespace App\Containers\User\Services;
4
5
use App\Containers\ApiAuthentication\Services\ApiAuthenticationService;
6
use App\Containers\User\Contracts\UserRepositoryInterface;
7
use App\Containers\User\Exceptions\AccountFailedException;
8
use App\Port\Service\Abstracts\Service;
9
use Exception;
10
use Illuminate\Support\Facades\Hash;
11
12
/**
13
 * Class CreateUserService.
14
 *
15
 * @author Mahmoud Zalt <[email protected]>
16
 */
17
class CreateUserService extends Service
18
{
19
20
    /**
21
     * @var \App\Containers\User\Contracts\UserRepositoryInterface
22
     */
23
    private $userRepository;
24
25
    /**
26
     * @var \App\Containers\ApiAuthentication\Services\ApiAuthenticationService
27
     */
28
    private $authenticationService;
29
30
    /**
31
     * CreateUserService constructor.
32
     *
33
     * @param \App\Containers\User\Contracts\UserRepositoryInterface              $userRepository
34
     * @param \App\Containers\ApiAuthentication\Services\ApiAuthenticationService $authenticationService
35
     */
36
    public function __construct(
37
        UserRepositoryInterface $userRepository,
38
        ApiAuthenticationService $authenticationService
39
    ) {
40
        $this->userRepository = $userRepository;
41
        $this->authenticationService = $authenticationService;
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 byCredentials($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->authenticationService->loginFromObject($user);
70
        }
71
72
        return $user;
73
    }
74
75
    /**
76
     * @param      $agentId device ID (example: iphone UUID, Android ID)
77
     * @param null $device
78
     * @param null $platform
79
     *
80
     * @return  mixed
81
     */
82
    public function byAgent($agentId, $device = null, $platform = null)
83
    {
84
        try {
85
            // create new user
86
            $user = $this->userRepository->create([
87
                'agent_id' => $agentId,
88
                'device'   => $device,
89
                'platform' => $platform,
90
            ]);
91
        } catch (Exception $e) {
92
            throw (new AccountFailedException())->debug($e);
93
        }
94
95
        return $user;
96
    }
97
98
}
99