Passed
Push — 5.0.0 ( 78f010...4a0fb0 )
by Fèvre
05:30
created

UserForm   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 163
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 48 3
A rules() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Validation\Rule;
9
use Livewire\Form;
10
use Xetaio\Mentions\Parser\MentionParser;
11
use Xetaravel\Models\Account;
12
use Xetaravel\Models\User;
13
use Throwable;
14
15
class UserForm extends Form
16
{
17
    /**
18
     * The category to update.
19
     *
20
     * @var User|null
21
     */
22
    public ?User $user = null;
23
24
    /**
25
     * The username of the user.
26
     *
27
     * @var string|null
28
     */
29
    public ?string $username = null;
30
31
    /**
32
     * The email of the user.
33
     *
34
     * @var string|null
35
     */
36
    public ?string $email = null;
37
38
    /**
39
     * The first_name of the user.
40
     *
41
     * @var string|null
42
     */
43
    public ?string $first_name = null;
44
45
    /**
46
     * The last_name of the user.
47
     *
48
     * @var string|null
49
     */
50
    public ?string $last_name = null;
51
52
    /**
53
     * The facebook of the user.
54
     *
55
     * @var string|null
56
     */
57
    public ?string $facebook = null;
58
59
    /**
60
     * The twitter of the user.
61
     *
62
     * @var string|null
63
     */
64
    public ?string $twitter = null;
65
66
    /**
67
     * The biography of the user.
68
     *
69
     * @var string|null
70
     */
71
    public ?string $biography = null;
72
73
    /**
74
     * The signature of the user.
75
     *
76
     * @var string|null
77
     */
78
    public ?string $signature = null;
79
80
    /**
81
     * The roles of the user.
82
     *
83
     * @var array
84
     */
85
    public array $roles = [];
86
87
    /**
88
     * The permissions of the user.
89
     *
90
     * @var array
91
     */
92
    public array $permissions = [];
93
94
    /**
95
     * Whatever the user have the by_pass permission.
96
     *
97
     * @var bool|null
98
     */
99
    public ?bool $can_bypass = null;
100
101
    protected function rules(): array
102
    {
103
        return [
104
            'username' => [
105
                'required',
106
                'alpha_num',
107
                'min:4',
108
                'max:20',
109
                Rule::unique('users')->ignore($this->user)
110
            ],
111
            'email' => [
112
                'required',
113
                'email',
114
                Rule::unique('users')->ignore($this->user)
115
            ],
116
            'first_name' => 'max:20',
117
            'last_name' => 'max:20',
118
            'facebook' => 'max:50',
119
            'twitter' => 'max:50'
120
        ];
121
    }
122
123
    /**
124
     * Function to update the model.
125
     *
126
     * @return User
127
     *
128
     * @throws Throwable
129
     */
130
    public function update(): User
131
    {
132
        return DB::transaction(function () {
133
            // Update the user
134
            $this->user->username = $this->username;
135
            $this->user->email = $this->email;
136
            $this->user->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

136
            $this->user->/** @scrutinizer ignore-call */ 
137
                         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...
137
138
            // Create or Update the account
139
            $account = Account::updateOrCreate(
140
                [
141
                    'user_id' => $this->user->getKey(),
142
                ],
143
                [
144
                    'first_name' => $this->first_name,
145
                    'last_name' => $this->last_name,
146
                    'facebook' => $this->facebook,
147
                    'twitter' => $this->twitter,
148
                    'biography' => $this->biography,
149
                    'signature' => $this->signature,
150
                ]
151
            );
152
153
            $parser = new MentionParser($account, [
0 ignored issues
show
Bug introduced by
It seems like $account can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $model of Xetaio\Mentions\Parser\M...onParser::__construct() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

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

153
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $account, [
Loading history...
154
                'regex' => config('mentions.regex'),
155
                'mention' => false
156
            ]);
157
            $signature = $parser->parse($account->signature);
0 ignored issues
show
Bug introduced by
The property signature does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
158
            $biography = $parser->parse($account->biography);
0 ignored issues
show
Bug introduced by
The property biography does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
159
160
            $account->signature = $signature;
161
            $account->biography = $biography;
162
            $account->save();
163
164
            $this->user->syncRoles($this->roles);
165
166
            // Link the selected permissions to the user.
167
            if (auth()->user()->can('assignDirectPermission', $this->user)) {
168
                $this->user->syncPermissions($this->permissions);
169
170
                if ($this->can_bypass) {
171
                    $this->user->givePermissionTo('bypass login');
172
                } else {
173
                    $this->user->revokePermissionTo('bypass login');
174
                }
175
            }
176
177
            return $this->user;
178
        });
179
    }
180
}
181