Completed
Push — master ( 2ea8ca...e6148c )
by Mahmoud
04:04
created

CreateUserService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A byCredentials() 0 18 2
A byVisitor() 0 11 1
A create() 0 11 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
        // create new user
57
        $user = $this->create([
58
            'name'     => $name,
59
            'email'    => $email,
60
            'password' => $hashedPassword,
61
        ]);
62
63
        if ($login) {
64
            // login this user using it's object and inject it's token on it
65
            $user = $this->authenticationService->loginFromObject($user);
66
        }
67
68
        return $user;
69
    }
70
71
    /**
72
     * @param      $visitorId device ID (example: iphone UUID, Android ID)
73
     * @param null $device
74
     * @param null $platform
75
     *
76
     * @return  mixed
77
     */
78
    public function byVisitor($visitorId, $device = null, $platform = null)
79
    {
80
        // create new user
81
        $user = $this->create([
82
            'visitor_id' => $visitorId,
83
            'device'     => $device,
84
            'platform'   => $platform,
85
        ]);
86
87
        return $user;
88
    }
89
90
91
    /**
92
     * @param $data
93
     *
94
     * @return  mixed
95
     */
96
    private function create($data)
97
    {
98
        try {
99
            // create new user
100
            $user = $this->userRepository->create($data);
101
        } catch (Exception $e) {
102
            throw (new AccountFailedException())->debug($e);
103
        }
104
105
        return $user;
106
    }
107
108
}
109