CrudTrait::updateMessagesSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
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 Mail;
17
use Tinyissue\Model\Project;
18
use Tinyissue\Model\User;
19
20
/**
21
 * CrudTrait is trait class containing the methods for adding/editing/deleting the User model.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 *
25
 * @property static $this
26
 */
27
trait CrudTrait
28
{
29
    /**
30
     * Add a new user.
31
     *
32
     * @param array $info
33
     *
34
     * @return bool
35
     */
36 1
    public function createUser(array $info)
37
    {
38
        $insert = [
39 1
            'email'     => $info['email'],
40 1
            'firstname' => $info['firstname'],
41 1
            'lastname'  => $info['lastname'],
42 1
            'role_id'   => $info['role_id'],
43 1
            'private'   => (boolean) $info['private'],
44 1
            'password'  => Hash::make($info['password']),
45 1
            'status'    => $info['status'],
46 1
            'language'  => app('tinyissue.settings')->getLanguage(),
47
        ];
48
49 1
        return $this->fill($insert)->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
    }
51
52
    /**
53
     * Soft deletes a user and empties the email.
54
     *
55
     * @return bool
56
     */
57 1
    public function delete()
58
    {
59 1
        $this->update([
0 ignored issues
show
Bug introduced by
The method update() does not exist on Tinyissue\Model\Traits\User\CrudTrait. Did you maybe mean updateSetting()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60 1
            'email'   => $this->email . '_deleted',
0 ignored issues
show
Bug introduced by
The property email does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
            'deleted' => User::DELETED_USERS,
62
        ]);
63 1
        Project\User::where('user_id', '=', $this->id)->delete();
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
65 1
        return true;
66
    }
67
68
    /**
69
     * Updates the users settings, validates the fields.
70
     *
71
     * @param array $info
72
     *
73
     * @return Eloquent\Model
74
     */
75 3
    public function updateSetting(array $info)
76
    {
77 3
        $update = array_intersect_key($info, array_flip([
78 3
            'email',
79
            'firstname',
80
            'lastname',
81
            'language',
82
            'password',
83
            'private',
84
            'status',
85
        ]));
86
87 3
        return $this->updateUser($update);
88
    }
89
90
    /**
91
     * Update the user.
92
     *
93
     * @param array $info
94
     *
95
     * @return Eloquent\Model
96
     */
97 4
    public function updateUser(array $info = [])
98
    {
99 4
        if ($info['password']) {
100 1
            $info['password'] = Hash::make($info['password']);
101 3
        } elseif (empty($info['password'])) {
102 3
            unset($info['password']);
103
        }
104
105 4
        return $this->update($info);
0 ignored issues
show
Bug introduced by
The method update() does not exist on Tinyissue\Model\Traits\User\CrudTrait. Did you maybe mean updateSetting()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
106
    }
107
108
    /**
109
     * Update user messages setting.
110
     *
111
     * @param array $input
112
     */
113 4
    public function updateMessagesSettings(array $input)
114
    {
115 4
        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...
116 4
            ->where('user_id', '=', $this->id)
117 4
            ->whereIn('project_id', array_keys($input))
118 4
            ->get()
119 4
            ->each(function (Project\User $project) use ($input) {
120 4
                $project->message_id = $input[$project->project_id];
121 4
                $project->save();
122 4
            });
123
    }
124
125
    /**
126
     * Fill the model with an array of attributes.
127
     *
128
     * @param array $attributes
129
     *
130
     * @return $this
131
     *
132
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
133
     */
134
    abstract public function fill(array $attributes);
135
}
136