AccountForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 47
c 2
b 0
f 0
dl 0
loc 121
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 39 2
A load() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\DB;
9
use Livewire\Attributes\Validate;
10
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
11
use Livewire\Form;
12
use Xetaio\Mentions\Parser\MentionParser;
13
use Xetaravel\Models\Account;
14
use Throwable;
15
use Xetaravel\Models\User;
16
17
class AccountForm extends Form
18
{
19
    /**
20
     * The last name of the user.
21
     *
22
     * @var string|null
23
     */
24
    #[Validate('max:50')]
25
    public ?string $last_name = null;
26
27
    /**
28
     * The first name of the user.
29
     *
30
     * @var string|null
31
     */
32
    #[Validate('max:50')]
33
    public ?string $first_name = null;
34
35
    /**
36
     * The twitter of the user.
37
     *
38
     * @var string|null
39
     */
40
    #[Validate('max:50')]
41
    public ?string $twitter = null;
42
43
    /**
44
     * The facebook of the user.
45
     *
46
     * @var string|null
47
     */
48
    #[Validate('max:50')]
49
    public ?string $facebook = null;
50
51
    /**
52
     * The biography of the user.
53
     *
54
     * @var string|null
55
     */
56
    #[Validate('max:2500')]
57
    public ?string $biography = null;
58
59
    /**
60
     * The signature of the user.
61
     *
62
     * @var string|null
63
     */
64
    #[Validate('max:350')]
65
    public ?string $signature = null;
66
67
    /**
68
     * The avatar of the user.
69
     *
70
     * @var TemporaryUploadedFile|null
71
     */
72
    #[Validate('nullable|image|max:10240')]
73
    public ?TemporaryUploadedFile $avatar = null;
74
75
    /**
76
     * Load the fields.
77
     *
78
     * @return void
79
     */
80
    public function load(): void
81
    {
82
        $user = Auth::user();
83
84
        $this->first_name = $user->first_name;
0 ignored issues
show
Bug introduced by
Accessing first_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
85
        $this->last_name = $user->last_name;
0 ignored issues
show
Bug introduced by
Accessing last_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
86
        $this->twitter = $user->twitter;
0 ignored issues
show
Bug introduced by
Accessing twitter on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
87
        $this->facebook = $user->facebook;
0 ignored issues
show
Bug introduced by
Accessing facebook on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
88
        $this->biography = $user->biography;
0 ignored issues
show
Bug introduced by
Accessing biography on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
89
        $this->signature = $user->signature;
0 ignored issues
show
Bug introduced by
Accessing signature on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
90
    }
91
92
    /**
93
     * Function to create the post.
94
     *
95
     * @return Account
96
     *
97
     * @throws Throwable
98
     */
99
    public function update(): Account
100
    {
101
        return DB::transaction(function () {
102
            $user = User::find(Auth::id());
103
104
            $account = Account::updateOrCreate(
105
                [
106
                    'user_id' => $user->getKey(),
107
                ],
108
                [
109
                    'first_name' => $this->first_name,
110
                    'last_name' => $this->last_name,
111
                    'facebook' => $this->facebook,
112
                    'twitter' => $this->twitter,
113
                    'biography' => $this->biography,
114
                    'signature' => $this->signature,
115
                ]
116
            );
117
118
            $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

118
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $account, [
Loading history...
119
                'regex' => config('mentions.regex'),
120
                'mention' => false
121
            ]);
122
            $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...
123
            $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...
124
125
            $account->signature = $signature;
126
            $account->biography = $biography;
127
            $account->save();
128
129
            if (!is_null($this->avatar)) {
130
                $user->clearMediaCollection('avatar');
131
                $user->addMedia($this->avatar->getRealPath())
132
                    ->setName(mb_substr(md5($user->username), 0, 10))
133
                    ->setFileName(mb_substr(md5($user->username), 0, 10) . '.' . $this->avatar->getClientOriginalExtension())
134
                    ->toMediaCollection('avatar');
135
            }
136
137
            return $account;
138
        });
139
    }
140
}
141