Passed
Push — main ( 51da02...cc341e )
by Usama
03:58
created

CommentPolicy::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Policies;
4
5
use Illuminate\Auth\Access\HandlesAuthorization;
6
use Illuminate\Auth\Access\Response;
7
use Usamamuneerchaudhary\Commentify\Models\Comment;
8
9
class CommentPolicy
10
{
11
    use HandlesAuthorization;
12
13
14
    /**
15
     * Determine whether the user can view any comments.
16
     * @param $user
17
     * @return Response
18
     */
19
    public function create($user): Response
20
    {
21
        // Check if the user is temporarily banned from commenting
22
        if (method_exists($user, 'isCommentBanned') && $user->isCommentBanned()) {
23
            return Response::deny(__('commentify::commentify.comments.banned_message'), 403);
24
        }
25
26
        return Response::allow();
27
    }
28
29
    /**
30
     * @param $user
31
     * @param  Comment  $comment
32
     * @return Response
33
     */
34
    public function update($user, Comment $comment): Response
35
    {
36
        if (method_exists($user, 'isCommentBanned') && $user->isCommentBanned()) {
37
            return Response::deny(__('commentify::commentify.comments.banned_message'), 403);
38
        }
39
        return $user->id === $comment->user_id
0 ignored issues
show
Bug introduced by
The property user_id does not exist on Usamamuneerchaudhary\Commentify\Models\Comment. Did you mean user?
Loading history...
40
            ? Response::allow()
41
            : Response::denyWithStatus(401);
42
    }
43
44
45
    /**
46
     * @param $user
47
     * @param  Comment  $comment
48
     * @return Response
49
     */
50
    public function destroy($user, Comment $comment): Response
51
    {
52
        if (method_exists($user, 'isCommentBanned') && $user->isCommentBanned()) {
53
            return Response::deny(__('commentify::commentify.comments.banned_message'), 403);
54
        }
55
        return $user->id === $comment->user_id
0 ignored issues
show
Bug introduced by
The property user_id does not exist on Usamamuneerchaudhary\Commentify\Models\Comment. Did you mean user?
Loading history...
56
            ? Response::allow()
57
            : Response::denyWithStatus(401);
58
    }
59
}
60