Passed
Push — master ( 408b41...cc81f2 )
by Adam
09:42
created

PostPolicy::delete()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Policies;
4
5
use Coyote\Reputation;
6
use Illuminate\Auth\Access\HandlesAuthorization;
7
use Coyote\User;
8
use Coyote\Post;
9
10
class PostPolicy
11
{
12
    use HandlesAuthorization;
13
14
    /**
15
     * @param User $user
16
     * @param Post $post
17
     * @return bool
18
     */
19
    public function update(User $user, Post $post): bool
20
    {
21
        if (!$this->isLocked($post)
22
            && $this->isAuthor($user, $post)
23
            && ($this->isRecentlyAdded($post) || $this->hasEnoughReputation($user, $post))
24
            && !$this->isArchive($post)) {
25
            return true;
26
        }
27
28
        return $this->check('forum-update', $user, $post);
29
    }
30
31
    /**
32
     * @param User $user
33
     * @param Post $post
34
     * @return bool
35
     */
36
    public function delete(User $user, Post $post): bool
37
    {
38
        if (!$this->isLocked($post)
39
            && $this->isAuthor($user, $post)
40
            && $this->hasEnoughReputation($user, $post)
41
            && !$this->isArchive($post)) {
42
            return true;
43
        }
44
45
        return $this->check('forum-delete', $user, $post);
46
    }
47
48
    public function accept(User $user, Post $post): bool
49
    {
50
        return $post->id !== $post->topic->first_post_id && ($user->id === $post->topic->firstPost->user_id || $user->can('update', $post->forum));
51
    }
52
53
    /**
54
     * @param string $ability
55
     * @param User $user
56
     * @param Post $post
57
     * @return bool
58
     */
59
    private function check(string $ability, User $user, Post $post): bool
60
    {
61
        return $user->can(substr($ability, 6), $post->forum);
62
    }
63
64
    private function isLocked(Post $post): bool
65
    {
66
        return $post->forum->is_locked // removing (updating etc) in locked category is forbidden
67
            || $post->topic->is_locked;
68
    }
69
70
    /**
71
     * @param User $user
72
     * @param Post $post
73
     * @return bool
74
     */
75
    private function isAuthor(User $user, Post $post): bool
76
    {
77
        return $user->id === $post->user_id;
78
    }
79
80
    /**
81
     * @param User $user
82
     * @param Post $post
83
     * @return bool
84
     */
85
    private function hasEnoughReputation(User $user, Post $post): bool
86
    {
87
        return $post->id == $post->topic->last_post_id || $user->reputation >= Reputation::DELETE_POSTS;
88
    }
89
90
    /**
91
     * @param Post $post
92
     * @return bool
93
     */
94
    private function isRecentlyAdded(Post $post): bool
95
    {
96
        return $post->created_at->diffInMinutes(now()) < 30;
97
    }
98
99
    private function isArchive(Post $post): bool
100
    {
101
        return $post->created_at->diffInMonths(now()) >= 1;
102
    }
103
}
104