Completed
Push — develop ( 4ae1ad...049b1a )
by Mohamed
08:18
created

CrudTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 2
cbo 4
dl 0
loc 85
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createUser() 0 23 1
A delete() 0 10 1
A updateSetting() 0 12 1
A updateUser() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\User;
13
14
use Hash;
15
use Illuminate\Database\Eloquent;
16
use Illuminate\Mail\Message as MailMessage;
17
use Illuminate\Support\Str;
18
use Mail;
19
use Tinyissue\Model\Project;
20
use Tinyissue\Model\User;
21
22
/**
23
 * CrudTrait is trait class containing the methods for adding/editing/deleting the User model
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int           $id
28
 * @property string        $email
29
 * @property string        $fullname
30
 *
31
 * @method   Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and')
32
 * @method   Eloquent\Model fill(array $attributes)
33
 * @method   Eloquent\Model update(array $attributes = array())
34
 */
35
trait CrudTrait
36
{
37
    /**
38
     * Add a new user.
39
     *
40
     * @param array $info
41
     *
42
     * @return bool
43
     */
44 1
    public function createUser(array $info)
45
    {
46
        $insert = [
47 1
            'email'     => $info['email'],
48 1
            'firstname' => $info['firstname'],
49 1
            'lastname'  => $info['lastname'],
50 1
            'role_id'   => $info['role_id'],
51 1
            'password'  => Hash::make($password = Str::random(6)),
52
        ];
53
54 1
        $this->fill($insert)->save();
55
56
        /* Send Activation email */
57
        $viewData = [
58 1
            'email'    => $info['email'],
59 1
            'password' => $password,
60
        ];
61 1
        Mail::send('email.new_user', $viewData, function (MailMessage $message) {
62 1
            $message->to($this->email, $this->fullname)->subject(trans('tinyissue.subject_your_account'));
63 1
        });
64
65 1
        return true;
66
    }
67
68
    /**
69
     * Soft deletes a user and empties the email
70
     *
71
     * @return bool
72
     */
73 1
    public function delete()
74
    {
75 1
        $this->update([
76 1
            'email'   => '',
77
            'deleted' => User::DELETED_USERS,
78
        ]);
79 1
        Project\User::where('user_id', '=', $this->id)->delete();
80
81 1
        return true;
82
    }
83
84
    /**
85
     * Updates the users settings, validates the fields.
86
     *
87
     * @param array $info
88
     *
89
     * @return Eloquent\Model
90
     */
91 2
    public function updateSetting(array $info)
92
    {
93 2
        $update = array_intersect_key($info, array_flip([
94 2
            'email',
95
            'firstname',
96
            'lastname',
97
            'language',
98
            'password',
99
        ]));
100
101 2
        return $this->updateUser($update);
102
    }
103
104
    /**
105
     * Update the user
106
     *
107
     * @param array $info
108
     *
109
     * @return Eloquent\Model
110
     */
111 3
    public function updateUser(array $info = [])
112
    {
113 3
        if ($info['password']) {
114 1
            $info['password'] = Hash::make($info['password']);
115
        }
116
117 3
        return $this->update($info);
118
    }
119
}
120