Passed
Push — 5.0.0 ( 49e1c0...87aae2 )
by Fèvre
06:22
created

BlogArticleForm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
dl 0
loc 158
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A searchCategories() 0 11 1
A update() 0 25 2
A create() 0 37 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Support\Collection;
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\BlogArticle;
14
use Xetaravel\Models\BlogCategory;
15
use Throwable;
16
17
class BlogArticleForm extends Form
18
{
19
    /**
20
     * The article to update.
21
     *
22
     * @var BlogArticle|null
23
     */
24
    public ?BlogArticle $blogArticle = null;
25
26
    /**
27
     * The title of the article.
28
     *
29
     * @var string|null
30
     */
31
    #[Validate('required|min:5')]
32
    public ?string $title = null;
33
34
    /**
35
     * The category of the article
36
     *
37
     * @var int|null
38
     */
39
    #[Validate('required|numeric|exists:blog_categories,id')]
40
    public ?int $blog_category_id = null;
41
42
    /**
43
     * The published date of the article.
44
     *
45
     * @var string|null
46
     */
47
    #[Validate('nullable|date_format:Y-m-d H:i')]
48
    public ?string $published_at = null;
49
50
    /**
51
     * The content of the post, only when creating.
52
     *
53
     * @var string|null
54
     */
55
    #[Validate('required|min:10')]
56
    public ?string $content = null;
57
58
    /**
59
     * The banner of the article.
60
     *
61
     * @var TemporaryUploadedFile|null
62
     */
63
    #[Validate('nullable|image|max:10240')]
64
    public ?TemporaryUploadedFile $banner = null;
65
66
    /**
67
     * The categories used in choice.
68
     *
69
     * @var Collection|array
70
     */
71
    public Collection|array $categoriesSearchable = [];
72
73
    /**
74
     * Function to store the model.
75
     *
76
     * @return BlogArticle
77
     *
78
     * @throws Throwable
79
     */
80
    public function create(): BlogArticle
81
    {
82
        return DB::transaction(function () {
83
            $properties = [
84
                'blog_category_id',
85
                'title',
86
                'content'
87
            ];
88
            if ($this->published_at) {
89
                $properties[] = 'published_at';
90
            }
91
92
            $blogArticle = BlogArticle::create($this->only($properties));
93
94
            $parser = new MentionParser($blogArticle, [
0 ignored issues
show
Bug introduced by
It seems like $blogArticle 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

94
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $blogArticle, [
Loading history...
95
                'regex' => config('mentions.regex')
96
            ]);
97
            $content = $parser->parse($blogArticle->content);
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
98
99
            $blogArticle->content = $content;
100
            $blogArticle->save();
101
102
            // Default banner for the article.
103
            $banner = public_path('images/articles/default_banner.jpg');
104
105
            if (!is_null($this->banner)) {
106
                $banner = $this->banner;
107
            }
108
109
            $blogArticle->clearMediaCollection('article');
110
            $blogArticle->addMedia($banner)
111
                ->preservingOriginal()
112
                ->setName(mb_substr(md5($blogArticle->title), 0, 10))
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
113
                ->setFileName(mb_substr(md5($blogArticle->title), 0, 10) . '.' . (is_string($banner) ? 'jpg' : $banner->getClientOriginalExtension()))
114
                ->toMediaCollection('article');
115
116
            return $blogArticle;
117
        });
118
    }
119
120
    /**
121
     * Function to update the article.
122
     *
123
     * @return BlogArticle
124
     *
125
     * @throws Throwable
126
     */
127
    public function update(): BlogArticle
128
    {
129
        return DB::transaction(function () {
130
            $this->blogArticle->title = $this->title;
131
            $this->blogArticle->blog_category_id = $this->blog_category_id;
132
            $this->blogArticle->published_at = $this->published_at;
133
134
            $parser = new MentionParser($this->blogArticle, [
0 ignored issues
show
Bug introduced by
It seems like $this->blogArticle can also be of type null; 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

134
            $parser = new MentionParser(/** @scrutinizer ignore-type */ $this->blogArticle, [
Loading history...
135
                'regex' => config('mentions.regex')
136
            ]);
137
            $content = $parser->parse($this->content);
138
139
            $this->blogArticle->content = $content;
140
            $this->blogArticle->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

140
            $this->blogArticle->/** @scrutinizer ignore-call */ 
141
                                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...
141
142
            if (!is_null($this->banner)) {
143
                $this->blogArticle->clearMediaCollection('article');
144
                $this->blogArticle->addMedia($this->banner)
145
                    ->preservingOriginal()
146
                    ->setName(mb_substr(md5($this->blogArticle->title), 0, 10))
0 ignored issues
show
Bug introduced by
It seems like $this->blogArticle->title can also be of type null; however, parameter $string of md5() does only seem to accept string, 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

146
                    ->setName(mb_substr(md5(/** @scrutinizer ignore-type */ $this->blogArticle->title), 0, 10))
Loading history...
147
                    ->setFileName(mb_substr(md5($this->blogArticle->title), 0, 10) . '.' . $this->banner->getClientOriginalExtension())
148
                    ->toMediaCollection('article');
149
            }
150
151
            return $this->blogArticle;
152
        });
153
154
155
    }
156
157
    /**
158
     * Function to search categories.
159
     *
160
     * @param string $value
161
     *
162
     * @return void
163
     */
164
    public function searchCategories(string $value = ''): void
165
    {
166
        $selectedOption = BlogCategory::where('id', $this->blog_category_id)->get();
167
168
        $categories = BlogCategory::query()
169
            ->where('title', 'like', "%$value%");
170
171
        $this->categoriesSearchable = $categories->take(10)
172
            ->orderBy('title')
0 ignored issues
show
Bug introduced by
'title' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

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

172
            ->orderBy(/** @scrutinizer ignore-type */ 'title')
Loading history...
173
            ->get()
174
            ->merge($selectedOption);
175
    }
176
}
177