AdminUser::onCreating()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yeelight\Models;
4
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
7
use Prettus\Repository\Traits\TransformableTrait;
8
use Yeelight\Services\Image\Models\Traits\YeelightHasOneImageTrait;
9
use Yeelight\Services\Image\Models\Traits\YeelightUploadOneImageTrait;
10
use Yeelight\Traits\HasPermissions;
11
12
/**
13
 * Class AdminUser
14
 *
15
 * @category Yeelight
16
 *
17
 * @package Yeelight\Models
18
 *
19
 * @author Sheldon Lee <[email protected]>
20
 *
21
 * @license https://opensource.org/licenses/MIT MIT
22
 *
23
 * @link https://www.yeelight.com
24
 */
25
class AdminUser extends BaseModel implements AuthenticatableContract
26
{
27
    use TransformableTrait,
0 ignored issues
show
Bug introduced by
The trait Yeelight\Services\Image\...eelightHasOneImageTrait requires the property $yeelight_image which is not provided by Yeelight\Models\AdminUser.
Loading history...
introduced by
The trait Yeelight\Traits\HasPermissions requires some properties which are not provided by Yeelight\Models\AdminUser: $permissions, $roles
Loading history...
28
        Authenticatable,
29
        HasPermissions,
30
        YeelightHasOneImageTrait,
31
        YeelightUploadOneImageTrait;
32
33
    /**
34
     * Indicates if the model should be auto set user_id.
35
     *
36
     * @var bool
37
     */
38
    protected $autoUserId = false;
39
40
    /**
41
     * Indicates if the model should be recorded ips.
42
     *
43
     * @var bool
44
     */
45
    protected $ips = false;
46
47
    /**
48
     * Indicates if the model should be recorded users.
49
     *
50
     * @var bool
51
     */
52
    protected $update_users = false;
53
54
    protected $primaryKey = 'id';
55
56
    protected $fillable = ['username', 'password', 'name', 'yeelight_image_id'];
57
58
    // Fields to be converted to Carbon object automatically
59
    protected $dates = [];
60
61
    /**
62
     * The attributes that should be hidden for arrays.
63
     *
64
     * @var array
65
     */
66
    protected $hidden = [
67
        'remember_token',
68
    ];
69
70
    /**
71
     * Create a new Eloquent model instance.
72
     *
73
     * @param array $attributes attributes
74
     */
75
    public function __construct(array $attributes = [])
76
    {
77
        $this->setTable(config('yeelight.backend.database.admin_users_table'));
78
79
        parent::__construct($attributes);
80
    }
81
82
    public function onCreating()
83
    {
84
        parent::onCreating();
85
86
        //密码
87
        $this->password = bcrypt($this->password);
0 ignored issues
show
Bug introduced by
The property password does not seem to exist on Yeelight\Models\AdminUser. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
88
89
        //头像
90
        $yeelightImage = $this->uploadOne('avatar');
91
        $this->yeelight_image_id = $yeelightImage ? $yeelightImage->getYeelightImageId() : null;
0 ignored issues
show
introduced by
$yeelightImage is of type Illuminate\Http\RedirectResponse, thus it always evaluated to true.
Loading history...
Bug introduced by
The property yeelight_image_id does not seem to exist on Yeelight\Models\AdminUser. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
92
    }
93
94
    public function onCreated()
95
    {
96
        parent::onCreated();
97
98
        //头像
99
        if ($this->yeelight_image_id) {
100
            $this->yeelight_image->user_id = $this->attributes[$this->getKeyName()];
0 ignored issues
show
Bug introduced by
The property user_id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug Best Practice introduced by
The property yeelight_image does not exist on Yeelight\Models\AdminUser. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The property user_id does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
Bug introduced by
The property user_id does not seem to exist on Illuminate\Support\Collection.
Loading history...
101
            $this->yeelight_image->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
            $this->yeelight_image->/** @scrutinizer ignore-call */ 
102
                                   save();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        }
103
104
        //权限
105
        $permissions = array_filter(request()->get('permissions', []));
106
        if (!empty($permissions)) {
107
            $this->permissions()->sync($permissions);
108
        }
109
110
        //角色
111
        $roles = array_filter(request()->get('roles', []));
112
        if (!empty($roles)) {
113
            $this->roles()->sync($roles);
114
        }
115
    }
116
117
    public function onUpdating()
118
    {
119
        parent::onUpdating();
120
121
        //密码
122
        if (request()->get('password')
123
            && $this->password != request()->get('password')
124
        ) {
125
            $this->password = bcrypt(request()->get('password'));
0 ignored issues
show
Bug introduced by
The property password does not seem to exist on Yeelight\Models\AdminUser. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
126
        }
127
128
        //头像
129
        $yeelightImage = $this->uploadOne('avatar');
130
        if ($yeelightImage) {
0 ignored issues
show
introduced by
$yeelightImage is of type Illuminate\Http\RedirectResponse, thus it always evaluated to true.
Loading history...
131
            $this->yeelight_image_id = $yeelightImage->getYeelightImageId();
0 ignored issues
show
Bug introduced by
The property yeelight_image_id does not seem to exist on Yeelight\Models\AdminUser. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
132
            $this->yeelight_image->user_id = $this->attributes[$this->getKeyName()];
0 ignored issues
show
Bug introduced by
The property user_id does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
Bug introduced by
The property user_id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug Best Practice introduced by
The property yeelight_image does not exist on Yeelight\Models\AdminUser. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The property user_id does not seem to exist on Illuminate\Support\Collection.
Loading history...
133
            $this->yeelight_image->save();
134
        }
135
136
        //权限
137
        $permissions = array_filter(request()->get('permissions', []));
138
        if (!empty($permissions)) {
139
            $this->permissions()->sync($permissions);
140
        }
141
142
        //角色
143
        $roles = array_filter(request()->get('roles', []));
144
        if (!empty($roles)) {
145
            $this->roles()->sync($roles);
146
        }
147
    }
148
149
    public function onDeleting()
150
    {
151
        parent::onDeleting();
152
153
        //权限
154
        $this->permissions()->sync([]);
155
156
        //角色
157
        $this->roles()->sync([]);
158
    }
159
}
160