Completed
Branch develop (a31570)
by Mohamed
08:09 queued 04:45
created

CrudTrait::updateSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 9
nc 1
nop 1
crap 1
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
            'private'   => (boolean)$info['private'],
52 1
            'password'  => Hash::make($password = Str::random(6)),
53
        ];
54
55 1
        $this->fill($insert)->save();
56
57
        /* Send Activation email */
58
        $viewData = [
59 1
            'email'    => $info['email'],
60 1
            'password' => $password,
61
        ];
62 1
        Mail::send('email.new_user', $viewData, function (MailMessage $message) {
63 1
            $message->to($this->email, $this->fullname)->subject(trans('tinyissue.subject_your_account'));
64 1
        });
65
66 1
        return true;
67
    }
68
69
    /**
70
     * Soft deletes a user and empties the email
71
     *
72
     * @return bool
73
     */
74 1
    public function delete()
75
    {
76 1
        $this->update([
77 1
            'email'   => '',
78
            'deleted' => User::DELETED_USERS,
79
        ]);
80 1
        Project\User::where('user_id', '=', $this->id)->delete();
81
82 1
        return true;
83
    }
84
85
    /**
86
     * Updates the users settings, validates the fields.
87
     *
88
     * @param array $info
89
     *
90
     * @return Eloquent\Model
91
     */
92 2
    public function updateSetting(array $info)
93
    {
94 2
        $update = array_intersect_key($info, array_flip([
95 2
            'email',
96
            'firstname',
97
            'lastname',
98
            'language',
99
            'password',
100
            'private',
101
        ]));
102
103 2
        return $this->updateUser($update);
104
    }
105
106
    /**
107
     * Update the user
108
     *
109
     * @param array $info
110
     *
111
     * @return Eloquent\Model
112
     */
113 3
    public function updateUser(array $info = [])
114
    {
115 3
        if ($info['password']) {
116 1
            $info['password'] = Hash::make($info['password']);
117
        }
118
119 3
        return $this->update($info);
120
    }
121
}
122