Passed
Push — 5.0.0 ( f10804...44b42e )
by Fèvre
15:04 queued 07:40
created

AccountForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A update() 0 47 2
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
    public ?string $last_name = null;
25
26
    /**
27
     * The first name of the user.
28
     *
29
     * @var string|null
30
     */
31
    public ?string $first_name = null;
32
33
    /**
34
     * The twitter of the user.
35
     *
36
     * @var string|null
37
     */
38
    public ?string $twitter = null;
39
40
    /**
41
     * The facebook of the user.
42
     *
43
     * @var string|null
44
     */
45
    public ?string $facebook = null;
46
47
    /**
48
     * The biography of the user.
49
     *
50
     * @var string|null
51
     */
52
    public ?string $biography = null;
53
54
    /**
55
     * The signature of the user.
56
     *
57
     * @var string|null
58
     */
59
    public ?string $signature = null;
60
61
    /**
62
     * The avatar of the user.
63
     *
64
     * @var TemporaryUploadedFile|null
65
     */
66
    #[Validate('image')]
67
    public $avatar;
68
69
    /**
70
     * Function to create the post.
71
     *
72
     * @return Account
73
     *
74
     * @throws Throwable
75
     */
76
    public function update(): Account
77
    {
78
        return DB::transaction(function () {
79
            $user = User::find(Auth::id());
80
81
            $account = Account::updateOrCreate(
82
                [
83
                    'user_id' => $user->getKey(),
84
                ],
85
                [
86
                    'user_id' => $user->getKey(),
87
                    'first_name' => $this->first_name,
88
                    'last_name' => $this->last_name,
89
                    'facebook' => $this->facebook,
90
                    'twitter' => $this->twitter,
91
                    'biography' => $this->biography,
92
                    'signature' => $this->signature,
93
                ]
94
            );
95
96
            $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

96
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $account, [
Loading history...
97
                'regex' => config('mentions.regex'),
98
                'mention' => false
99
            ]);
100
            $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...
101
            $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...
102
103
            $account->signature = $signature;
104
            $account->biography = $biography;
105
            $account->save();
106
107
            //$this->avatar->store(path: 'photos');
108
109
            //dd(storage_path($this->avatar->store(path: 'private/livewire-tmp')));
110
111
            if (!is_null($this->avatar)) {
112
                //if ($file = storage_path($this->avatar->store(path: 'photos'))) {
113
                    $user->clearMediaCollection('avatar');
114
                    $user->addMediaFromDisk($this->avatar->getRealPath())
115
                        //->preservingOriginal()
116
                        ->setName(mb_substr(md5($user->username), 0, 10))
117
                        ->setFileName(mb_substr(md5($user->username), 0, 10))
118
                        ->toMediaCollection('avatar');
119
                //}
120
            }
121
122
            return $account;
123
        });
124
    }
125
}
126