Completed
Push — develop ( f7252d...97603d )
by Mohamed
04:44
created

CrudTrait::updateSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 4
cp 0
rs 9.4286
cc 1
eloc 8
nc 1
nop 1
crap 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
    public function createUser(array $info)
45
    {
46
        $insert = [
47
            'email'     => $info['email'],
48
            'firstname' => $info['firstname'],
49
            'lastname'  => $info['lastname'],
50
            'role_id'   => $info['role_id'],
51
            'password'  => Hash::make($password = Str::random(6)),
52
        ];
53
54
        $this->fill($insert)->save();
55
56
        /* Send Activation email */
57
        $viewData = [
58
            'email'    => $info['email'],
59
            'password' => $password,
60
        ];
61
        Mail::send('email.new_user', $viewData, function (MailMessage $message) {
62
            $message->to($this->email, $this->fullname)->subject(trans('tinyissue.subject_your_account'));
63
        });
64
65
        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
    public function updateSetting(array $info)
92
    {
93
        $update = array_intersect_key($info, array_flip([
94
            'email',
95
            'firstname',
96
            'lastname',
97
            'language',
98
            'password',
99
        ]));
100
101
        return $this->updateUser($update);
102
    }
103
104
    /**
105
     * Update the user
106
     *
107
     * @param array $info
108
     *
109
     * @return Eloquent\Model
110
     */
111
    public function updateUser(array $info = [])
112
    {
113
        if ($info['password']) {
114
            $info['password'] = Hash::make($info['password']);
115
        }
116
117
        return $this->update($info);
118
    }
119
}
120