1 | <?php |
||
2 | |||
3 | namespace FaithGen\SDK\Helpers; |
||
4 | |||
5 | use FaithGen\SDK\Http\Resources\Comment as CommentsResource; |
||
6 | use Illuminate\Http\Request; |
||
7 | use InnoFlash\LaraStart\Helper; |
||
0 ignored issues
–
show
|
|||
8 | |||
9 | class CommentHelper |
||
10 | { |
||
11 | /** |
||
12 | * Creates a comment for the given model. |
||
13 | * |
||
14 | * @param $model |
||
15 | * @param Request $request |
||
16 | * |
||
17 | * @return \Illuminate\Http\JsonResponse |
||
18 | */ |
||
19 | public static function createComment($model, Request $request) |
||
20 | { |
||
21 | try { |
||
22 | if ($user = auth('web')->user()) { |
||
23 | if ($user->active) { |
||
0 ignored issues
–
show
|
|||
24 | $comment = $model->comments()->create([ |
||
25 | 'comment' => $request->comment, |
||
26 | 'creatable_id' => auth('web')->user()->id, |
||
0 ignored issues
–
show
|
|||
27 | 'creatable_type' => get_class(auth('web')->user()), |
||
28 | ]); |
||
29 | } else { |
||
30 | abort(403, 'You are not permitted to comment on this'); |
||
31 | } |
||
32 | } else { |
||
33 | $comment = $model->comments()->create([ |
||
34 | 'comment' => $request->comment, |
||
35 | 'creatable_id' => auth()->user()->id, |
||
36 | 'creatable_type' => get_class(auth()->user()), |
||
37 | ]); |
||
38 | } |
||
39 | |||
40 | return response()->json([ |
||
41 | 'success' => true, |
||
42 | 'message' => 'Comment posted', |
||
43 | 'comment' => new CommentsResource($comment), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
44 | ], 201); |
||
45 | } catch (\Exception $e) { |
||
46 | abort(500, $e->getMessage()); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Gets the comments for the given model. |
||
52 | * |
||
53 | * @param $model |
||
54 | * @param Request $request |
||
55 | * |
||
56 | * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
||
57 | */ |
||
58 | public static function getComments($model, Request $request) |
||
59 | { |
||
60 | $comments = $model->comments() |
||
61 | ->latest() |
||
62 | ->with(['creatable.image']) |
||
63 | ->paginate(Helper::getLimit($request)); |
||
64 | |||
65 | CommentsResource::wrap('comments'); |
||
66 | |||
67 | return CommentsResource::collection($comments); |
||
68 | } |
||
69 | } |
||
70 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: