SermonPolicy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 4
b 0
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 3 1
A update() 0 3 1
A create() 0 12 3
A delete() 0 3 1
1
<?php
2
3
namespace FaithGen\Sermons\Policies;
4
5
use FaithGen\SDK\Models\Ministry;
6
use FaithGen\Sermons\Models\Sermon;
7
use FaithGen\Sermons\SermonHelper;
8
use Illuminate\Auth\Access\HandlesAuthorization;
9
10
class SermonPolicy
11
{
12
    use HandlesAuthorization;
13
14
    /**
15
     * Determine whether the user can view the sermon.
16
     *
17
     * @param  Ministry  $user
18
     * @param  Sermon  $sermon
19
     *
20
     * @return mixed
21
     */
22
    public function view(Ministry $user, Sermon $sermon)
23
    {
24
        return $user->id === $sermon->ministry_id;
25
    }
26
27
    /**
28
     * Determine whether the user can create sermons.
29
     *
30
     * @param  Ministry  $user
31
     *
32
     * @return mixed
33
     */
34
    public function create(Ministry $user)
35
    {
36
        if ($user->account->level !== 'Free') {
37
            return true;
38
        } else {
39
            $sermonsCount = Sermon::where('ministry_id', $user->id)
40
                ->whereBetween('created_at', [now()->firstOfMonth(), now()->lastOfMonth()])
41
                ->count();
42
            if ($sermonsCount >= SermonHelper::$freeSermonsCount) {
43
                return false;
44
            } else {
45
                return true;
46
            }
47
        }
48
    }
49
50
    /**
51
     * Determine whether the user can update the sermon.
52
     *
53
     * @param  Ministry  $user
54
     * @param  Sermon  $sermon
55
     *
56
     * @return mixed
57
     */
58
    public function update(Ministry $user, Sermon $sermon)
59
    {
60
        return $user->id === $sermon->ministry_id;
61
    }
62
63
    /**
64
     * Determine whether the user can delete the sermon.
65
     *
66
     * @param  Ministry  $user
67
     * @param  Sermon  $sermon
68
     *
69
     * @return mixed
70
     */
71
    public static function delete(Ministry $user, Sermon $sermon)
72
    {
73
        return $user->id === $sermon->ministry_id;
74
    }
75
}
76