UserRegistration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 5
rs 9.4286
c 1
b 0
f 1
cc 1
eloc 3
nc 1
nop 2
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