PostsController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
nc 1
nop 0
dl 0
loc 7
c 1
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace Wingsline\Blog\Http\Controllers;
4
5
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6
use Illuminate\Foundation\Bus\DispatchesJobs;
7
use Illuminate\Foundation\Validation\ValidatesRequests;
8
use Illuminate\Routing\Controller as BaseController;
9
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Exceptions\FileCannotBeAdded was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Wingsline\Blog\Http\Requests\ImageUploadRequest;
11
use Wingsline\Blog\Http\Requests\PostRequest;
12
use Wingsline\Blog\Posts\Post;
13
14
class PostsController extends BaseController
15
{
16
    use AuthorizesRequests;
17
    use DispatchesJobs;
18
    use ValidatesRequests;
19
20
    /**
21
     * Show the form for creating a new resource.
22
     *
23
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
24
     */
25
    public function create()
26
    {
27
        $post = new Post();
28
29
        $post->publish_date = now();
0 ignored issues
show
Bug Best Practice introduced by
The property publish_date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
31
        return view('blog::posts.create', compact('post'));
32
    }
33
34
    /**
35
     * Remove the specified resource from storage.
36
     *
37
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
38
     * @throws \Exception
39
     */
40
    public function destroy(Post $post)
41
    {
42
        $post->delete();
43
44
        flash()->success('Post deleted.');
45
46
        return redirect()->route('admin.posts.index');
47
    }
48
49
    /**
50
     * Show the form for editing the specified resource.
51
     *
52
     * @param int $id
53
     *
54
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
55
     */
56
    public function edit(Post $post)
57
    {
58
        return view('blog::posts.edit', compact('post'));
59
    }
60
61
    /**
62
     * Display a listing of the resource.
63
     *
64
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
65
     */
66
    public function index()
67
    {
68
        $posts = Post::orderBy('publish_date',
69
            'desc')->paginate(config('blog.per_page'));
70
71
        return view('blog::posts.index', compact('posts'));
72
    }
73
74
    /**
75
     * Generates a markdown preview for the post.
76
     *
77
     * @return array
78
     */
79
    public function preview(Post $post)
80
    {
81
        $post->text = request('payload', '');
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
83
        return ['data' => ['html' => $post->text]];
84
    }
85
86
    /**
87
     * Store a newly created resource in storage.
88
     *
89
     * @param \Illuminate\Http\Request $request
90
     *
91
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
92
     */
93
    public function store(PostRequest $request)
94
    {
95
        $post = (new Post())->updateAttributes($request->validated());
96
97
        flash()->success('Post saved.');
98
99
        return redirect()->route('admin.posts.edit', $post);
100
    }
101
102
    /**
103
     * Update the specified resource in storage.
104
     *
105
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
106
     */
107
    public function update(PostRequest $request, Post $post)
108
    {
109
        $post->updateAttributes($request->validated());
110
111
        flash()->success('Post updated.');
112
113
        return redirect()->route('admin.posts.edit', $post);
114
    }
115
116
    /**
117
     * Upload an image.
118
     *
119
     * @return array|\Illuminate\Http\JsonResponse
120
     */
121
    public function upload(Post $post, ImageUploadRequest $request)
122
    {
123
        try {
124
            $media = $post->addMedia($request->file('image'))
0 ignored issues
show
Bug introduced by
It seems like $request->file('image') can also be of type Illuminate\Http\UploadedFile[] and array; however, parameter $file of Wingsline\Blog\Posts\Post::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

124
            $media = $post->addMedia(/** @scrutinizer ignore-type */ $request->file('image'))
Loading history...
125
                ->toMediaCollection('images');
126
        } catch (FileCannotBeAdded $exception) {
127
            return response()->json(['error' => $exception->getMessage()], 422);
128
        }
129
130
        return [
131
            'data' => [
132
                'filePath' => ltrim(
133
                    parse_url($media->getFullUrl(),
134
                    PHP_URL_PATH),
135
                    '/'
136
                ),
137
            ],
138
        ];
139
    }
140
}
141