Passed
Push — test-livewire ( 4ad23d...72c8bb )
by Fèvre
06:14
created

ArticleController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\Blog;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Str;
7
use Illuminate\View\View;
8
use Xetaio\Mentions\Parser\MentionParser;
9
use Xetaravel\Http\Controllers\Admin\Controller;
10
use Xetaravel\Models\Article;
11
use Xetaravel\Models\Category;
12
use Xetaravel\Models\Repositories\ArticleRepository;
13
use Xetaravel\Models\Validators\ArticleValidator;
14
15
class ArticleController extends Controller
16
{
17
    /**
18
     * Constructor.
19
     */
20
    public function __construct()
21
    {
22
        parent::__construct();
23
24
        $this->breadcrumbs->addCrumb(
25
            '<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-2" fill="none"' .
26
            ' viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" ' .
27
            'stroke-linejoin="round" d="M19 20H5a2 2 0 01-2-2V6a2 2 0 012-2h10a2 2 0 012 2v1m2 13a2 2 0 01-2-2V7m2' .
28
            ' 13a2 2 0 002-2V9a2 2 0 00-2-2h-2m-4-3H9M7 16h6M7 8h6v4H7V8z" /></svg> Blog',
29
            route('admin.blog.article.index')
30
        );
31
    }
32
33
    /**
34
     * Show all articles.
35
     *
36
     * @return \Illuminate\View\View
37
     */
38
    public function index(): View
39
    {
40
        $this->breadcrumbs->addCrumb(
41
            '<i class="fa-regular fa-newspaper mr-2"></i> Manage Articles',
42
            route('admin.blog.article.index')
43
        );
44
45
        return view('Admin::Blog.article.index', ['breadcrumbs' => $this->breadcrumbs]);
46
    }
47
48
    /**
49
     * Show the article create form.
50
     *
51
     * @return \Illuminate\View\View
52
     */
53
    public function showCreateForm(): View
54
    {
55
        $categories = Category::pluck('title', 'id');
56
57
        $breadcrumbs = $this->breadcrumbs
58
            ->addCrumb(
59
                '<i class="fa-regular fa-newspaper mr-2"></i> Manage Articles',
60
                route('admin.blog.article.index')
61
            )
62
            ->addCrumb(
63
                '<i class="fa-solid fa-pencil mr-2"></i> Create',
64
                route('admin.blog.article.create')
65
            );
66
67
        return view('Admin::Blog.article.create', compact('categories', 'breadcrumbs'));
68
    }
69
70
    /**
71
     * Handle an article create request for the application.
72
     *
73
     * @param \Illuminate\Http\Request $request
74
     *
75
     * @return \Illuminate\Http\RedirectResponse
76
     */
77
    public function create(Request $request): RedirectResponse
78
    {
79
        ArticleValidator::create($request->all())->validate();
80
        $article = ArticleRepository::create($request->all());
81
82
        $parser = new MentionParser($article);
83
        $content = $parser->parse($article->content);
84
85
        $article->content = $content;
86
        $article->save();
87
88
        // Default banner for the article.
89
        $banner = public_path('images/articles/default_banner.jpg');
90
91
        if (!is_null($request->file('banner'))) {
92
            $banner = $request->file('banner');
93
        }
94
95
        $article->clearMediaCollection('article');
96
        $article->addMedia($banner)
0 ignored issues
show
Bug introduced by
It seems like $banner can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $file of Xetaravel\Models\Article::addMedia() does only seem to accept Symfony\Component\HttpFo...ile\UploadedFile|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

96
        $article->addMedia(/** @scrutinizer ignore-type */ $banner)
Loading history...
97
            ->preservingOriginal()
98
            ->setName(substr(md5($article->slug), 0, 10))
99
            ->setFileName(
100
                substr(md5($article->slug), 0, 10) . '.' . (is_string($banner) ? 'jpg'  : $banner->extension())
101
            )
102
            ->toMediaCollection('article');
103
104
        return redirect()
105
            ->route('admin.blog.article.index')
106
            ->with('success', 'Your article has been created successfully !');
107
    }
108
109
    /**
110
     * Show the article update form.
111
     *
112
     * @param string $slug The slug of the article.
113
     * @param int $id The id of the article.
114
     *
115
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
116
     */
117
    public function showUpdateForm(string $slug, int $id)
118
    {
119
        $article = Article::with('category', 'user', 'comments')
120
            ->where('id', $id)
121
            ->first();
122
123
        if (is_null($article)) {
124
            return redirect()
125
                ->route('admin.blog.article.index')
126
                ->with('danger', 'This article doesn\'t exist or has been deleted !');
127
        }
128
        $categories = Category::pluck('title', 'id');
129
130
        $breadcrumbs = $this->breadcrumbs
131
            ->addCrumb(
132
                '<i class="fa-regular fa-newspaper mr-2"></i> Manage Articles',
133
                route('admin.blog.article.index')
134
            )
135
            ->addCrumb(
136
                '<i class="fa-solid fa-pen-to-square mr-2"></i> Update : ' . e(Str::limit($article->title, 30)),
137
                route(
138
                    'admin.blog.article.index',
139
                    ['slug' => $article->category->slug, 'id' => $article->category->id]
140
                )
141
            );
142
143
        return view('Admin::Blog.article.update', compact('article', 'breadcrumbs', 'categories'));
144
    }
145
146
    /**
147
     * Handle an article update request for the application.
148
     *
149
     * @param \Illuminate\Http\Request $request
150
     * @param int $id The id of the article.
151
     *
152
     * @return \Illuminate\Http\RedirectResponse
153
     */
154
    public function update(Request $request, int $id): RedirectResponse
155
    {
156
        $article = Article::findOrFail($id);
157
158
        ArticleValidator::update($request->all(), $id)->validate();
159
        $article = ArticleRepository::update($request->all(), $article);
0 ignored issues
show
Bug introduced by
It seems like $article can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $article of Xetaravel\Models\Reposit...cleRepository::update() does only seem to accept Xetaravel\Models\Article, 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

159
        $article = ArticleRepository::update($request->all(), /** @scrutinizer ignore-type */ $article);
Loading history...
160
161
        $parser = new MentionParser($article);
162
        $content = $parser->parse($article->content);
163
164
        $article->content = $content;
165
        $article->save();
166
167
        if (!is_null($request->file('banner')) || $article->article_banner == '/images/articles/default_banner.jpg') {
168
            // Default banner for the article.
169
            $banner = public_path('images/articles/default_banner.jpg');
170
171
            if (!is_null($request->file('banner'))) {
172
                $banner = $request->file('banner');
173
            }
174
175
            $article->clearMediaCollection('article');
176
            $article->addMedia($banner)
0 ignored issues
show
Bug introduced by
It seems like $banner can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $file of Xetaravel\Models\Article::addMedia() does only seem to accept Symfony\Component\HttpFo...ile\UploadedFile|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

176
            $article->addMedia(/** @scrutinizer ignore-type */ $banner)
Loading history...
177
                ->preservingOriginal()
178
                ->setName(substr(md5($article->slug), 0, 10))
179
                ->setFileName(
180
                    substr(md5($article->slug), 0, 10) . '.' . (is_string($banner) ? 'jpg'  : $banner->extension())
181
                )
182
                ->toMediaCollection('article');
183
        }
184
185
186
187
        return redirect()
188
            ->route('admin.blog.article.index')
189
            ->with('success', 'Your article has been updated successfully !');
190
    }
191
}
192