Completed
Push — develop ( 1c3d8e...dc7d05 )
by Mohamed
07:44
created

CrudTrait::updateSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 14
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 10
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 1
            'status'    => $info['status'],
54
        ];
55
56 1
        $this->fill($insert)->save();
57
58
        /* Send Activation email */
59
        $viewData = [
60 1
            'email'    => $info['email'],
61 1
            'password' => $password,
62
        ];
63
        Mail::send('email.new_user', $viewData, function (MailMessage $message) {
64 1
            $message->to($this->email, $this->fullname)->subject(trans('tinyissue.subject_your_account'));
65 1
        });
66
67 1
        return true;
68
    }
69
70
    /**
71
     * Soft deletes a user and empties the email.
72
     *
73
     * @return bool
74
     */
75 1
    public function delete()
76
    {
77 1
        $this->update([
78 1
            'email'   => '',
79
            'deleted' => User::DELETED_USERS,
80
        ]);
81 1
        Project\User::where('user_id', '=', $this->id)->delete();
82
83 1
        return true;
84
    }
85
86
    /**
87
     * Updates the users settings, validates the fields.
88
     *
89
     * @param array $info
90
     *
91
     * @return Eloquent\Model
92
     */
93 3
    public function updateSetting(array $info)
94
    {
95 3
        $update = array_intersect_key($info, array_flip([
96 3
            'email',
97
            'firstname',
98
            'lastname',
99
            'language',
100
            'password',
101
            'private',
102
            'status',
103
        ]));
104
105 3
        return $this->updateUser($update);
106
    }
107
108
    /**
109
     * Update the user.
110
     *
111
     * @param array $info
112
     *
113
     * @return Eloquent\Model
114
     */
115 4
    public function updateUser(array $info = [])
116
    {
117 4
        if ($info['password']) {
118 1
            $info['password'] = Hash::make($info['password']);
119 3
        } elseif (empty($info['password'])) {
120 3
            unset($info['password']);
121
        }
122
123 4
        return $this->update($info);
124
    }
125
126
    /**
127
     * Update user messages setting.
128
     *
129
     * @param array $input
130
     */
131 3
    public function updateMessagesSettings(array $input)
132
    {
133 3
        return (new Project\User())
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Tinyissue\Model\Project\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
134 3
            ->where('user_id', '=', $this->id)
135 3
            ->whereIn('project_id', array_keys($input))
136 3
            ->get()
137 3
            ->each(function (Project\User $project) use ($input) {
138 3
                $project->message_id = $input[$project->project_id];
139 3
                $project->save();
140 3
            });
141
    }
142
}
143