UserRegistration   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 73
rs 10
c 2
b 0
f 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 16 2
A createUser() 0 4 1
A assignUserToUsersGroup() 0 6 1
A hasProfileData() 0 4 1
A createProfileForUser() 0 5 1
1
<?php namespace Modules\User\Services;
2
3
use Modules\Core\Contracts\Authentication;
4
use Modules\User\Events\UserHasRegistered;
5
use Modules\User\Repositories\RoleRepository;
6
7
class UserRegistration
8
{
9
    /**
10
     * @var Authentication
11
     */
12
    private $auth;
13
    /**
14
     * @var RoleRepository
15
     */
16
    private $role;
17
    /**
18
     * @var array
19
     */
20
    private $input;
21
22
    public function __construct(Authentication $auth, RoleRepository $role)
23
    {
24
        $this->auth = $auth;
25
        $this->role = $role;
26
    }
27
28
    /**
29
     * @param array $input
30
     * @return mixed
31
     */
32
    public function register(array $input)
33
    {
34
        $this->input = $input;
35
36
        $user = $this->createUser();
37
38
        if ($this->hasProfileData()) {
39
            $this->createProfileForUser($user);
40
        }
41
42
        $this->assignUserToUsersGroup($user);
43
44
        event(new UserHasRegistered($user));
45
46
        return $user;
47
    }
48
49
    private function createUser()
50
    {
51
        return $this->auth->register((array) $this->input);
52
    }
53
54
    private function assignUserToUsersGroup($user)
55
    {
56
        $role = $this->role->findByName('User');
57
58
        $this->auth->assignRole($user, $role);
59
    }
60
61
    /**
62
     * Check if the request input has a profile key
63
     * @return bool
64
     */
65
    private function hasProfileData()
66
    {
67
        return isset($this->input['profile']);
68
    }
69
70
    /**
71
     * Create a profile for the given user
72
     * @param $user
73
     */
74
    private function createProfileForUser($user)
75
    {
76
        $profileData = array_merge($this->input['profile'], ['user_id' => $user->id]);
77
        app('Modules\Profile\Repositories\ProfileRepository')->create($profileData);
78
    }
79
}
80