Issues (42)

src/Http/Controllers/SermonController.php (3 issues)

1
<?php
2
3
namespace FaithGen\Sermons\Http\Controllers;
4
5
use FaithGen\SDK\Helpers\CommentHelper;
6
use FaithGen\SDK\Http\Requests\CommentRequest;
7
use FaithGen\Sermons\Http\Requests\CreateRequest;
8
use FaithGen\Sermons\Http\Requests\GetRequest;
9
use FaithGen\Sermons\Http\Requests\IndexRequest;
10
use FaithGen\Sermons\Http\Requests\UpdatePictureRequest;
11
use FaithGen\Sermons\Http\Requests\UpdateRequest;
12
use FaithGen\Sermons\Http\Resources\Sermon as SermonResource;
13
use FaithGen\Sermons\Http\Resources\SermonList as ListResource;
14
use FaithGen\Sermons\Jobs\MessageFollowers;
15
use FaithGen\Sermons\Jobs\ProcessImage;
16
use FaithGen\Sermons\Jobs\S3Upload;
17
use FaithGen\Sermons\Jobs\UploadImage;
18
use FaithGen\Sermons\Models\Sermon;
19
use FaithGen\Sermons\SermonService;
20
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
21
use Illuminate\Foundation\Bus\DispatchesJobs;
22
use Illuminate\Foundation\Validation\ValidatesRequests;
23
use Illuminate\Http\Request;
24
use Illuminate\Routing\Controller;
25
use InnoFlash\LaraStart\Helper;
26
use InnoFlash\LaraStart\Traits\APIResponses;
27
28
class SermonController extends Controller
29
{
30
    use AuthorizesRequests, ValidatesRequests, APIResponses, DispatchesJobs;
31
    /**
32
     * @var SermonService
33
     */
34
    private $sermonService;
35
36
    public function __construct(SermonService $sermonService)
37
    {
38
        $this->sermonService = $sermonService;
39
    }
40
41
    public function create(CreateRequest $request)
42
    {
43
        return $this->sermonService->createFromParent($request->validated());
44
    }
45
46
    /**
47
     * Fetches a list of sermons.
48
     *
49
     * @param IndexRequest $request
50
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
51
     */
52
    public function index(IndexRequest $request)
53
    {
54
        $sermons = auth()->user()
55
            ->sermons()
56
            ->latest()
57
            ->where(fn ($sermon) => $sermon->search(['preacher', 'title', 'preacher'], $request->filter_text))
58
            ->paginate(Helper::getLimit($request));
59
60
        SermonResource::wrap('sermons');
61
62
        if ($request->has('full_sermons')) {
63
            return SermonResource::collection($sermons);
64
        } else {
65
            return ListResource::collection($sermons);
66
        }
67
    }
68
69
    /**
70
     * Get a single sermon details.
71
     *
72
     * @param Sermon $sermon
73
     * @return SermonResource
74
     * @throws \Illuminate\Auth\Access\AuthorizationException
75
     */
76
    public function view(Sermon $sermon)
77
    {
78
        $this->authorize('view', $sermon);
79
80
        SermonResource::withoutWrapping();
81
82
        return new SermonResource($sermon);
83
    }
84
85
    /**
86
     * Update preacher image.
87
     *
88
     * @param UpdatePictureRequest $request
89
     * @return mixed
90
     */
91
    public function updatePicture(UpdatePictureRequest $request)
92
    {
93
        if ($this->sermonService->getSermon()->image()->exists()) {
94
            try {
95
                $this->sermonService->deleteFiles($this->sermonService->getSermon());
96
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
97
            }
98
        }
99
100
        if ($request->hasImage && $request->has('image')) {
101
            try {
102
                MessageFollowers::withChain([
103
                    new UploadImage($this->sermonService->getSermon(), request('image')),
0 ignored issues
show
It seems like request('image') can also be of type array; however, parameter $image of FaithGen\Sermons\Jobs\UploadImage::__construct() 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

103
                    new UploadImage($this->sermonService->getSermon(), /** @scrutinizer ignore-type */ request('image')),
Loading history...
104
                    new ProcessImage($this->sermonService->getSermon()),
105
                    new S3Upload($this->sermonService->getSermon()),
106
                ])->dispatch($this->sermonService->getSermon());
107
108
                return $this->successResponse('Preacher image updated successfully!');
109
            } catch (\Exception $e) {
110
                abort(500, $e->getMessage());
111
            }
112
        } else {
113
            try {
114
                $this->sermonService->getSermon()->image()->delete();
115
116
                return $this->successResponse('Preacher image deleted successfully!');
117
            } catch (\Exception $e) {
118
                abort(500, $e->getMessage());
119
            }
120
        }
121
    }
122
123
    /**
124
     * Delete sermon.
125
     *
126
     * @param GetRequest $request
127
     * @return mixed
128
     * @throws \Illuminate\Auth\Access\AuthorizationException
129
     */
130
    public function delete(GetRequest $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

130
    public function delete(/** @scrutinizer ignore-unused */ GetRequest $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        $this->authorize('delete', $this->sermonService->getSermon());
133
134
        return $this->sermonService->destroy('Sermon deleted');
135
    }
136
137
    /**
138
     * Update sermon.
139
     *
140
     * @param UpdateRequest $request
141
     * @return \Illuminate\Http\JsonResponse|mixed
142
     */
143
    public function update(UpdateRequest $request)
144
    {
145
        return $this->sermonService->update($request->validated(), 'Sermon updated successfully');
146
    }
147
148
    /**
149
     * Comment a sermon.
150
     *
151
     * @param CommentRequest $request
152
     * @return \Illuminate\Http\JsonResponse
153
     */
154
    public function comment(CommentRequest $request)
155
    {
156
        return CommentHelper::createComment($this->sermonService->getSermon(), $request);
157
    }
158
159
    /**
160
     * Sermons posted to a sermon.
161
     *
162
     * @param Request $request
163
     * @param Sermon $sermon
164
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
165
     * @throws \Illuminate\Auth\Access\AuthorizationException
166
     */
167
    public function comments(Request $request, Sermon $sermon)
168
    {
169
        $this->authorize('view', $sermon);
170
171
        return CommentHelper::getComments($sermon, $request);
172
    }
173
}
174