CommentHelper::getComments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 2
b 0
f 1
nc 1
nop 2
dl 0
loc 10
rs 10
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
Bug introduced by
This use statement conflicts with another class in this namespace, FaithGen\SDK\Helpers\Helper. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
Bug introduced by
Accessing active on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
24
                    $comment = $model->comments()->create([
25
                        'comment'        => $request->comment,
26
                        'creatable_id'   => auth('web')->user()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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
The variable $comment does not seem to be defined for all execution paths leading up to this point.
Loading history...
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