Completed
Push — master ( d1df20...99f755 )
by Ashley
01:55
created

Manager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A redeem() 0 8 1
A check() 0 11 2
A lookupInvite() 0 8 2
B validateInvite() 0 14 7
1
<?php
2
3
namespace Clarkeash\Doorman;
4
5
use Clarkeash\Doorman\Exceptions\DoormanException;
6
use Clarkeash\Doorman\Exceptions\ExpiredInviteCode;
7
use Clarkeash\Doorman\Exceptions\InvalidInviteCode;
8
use Clarkeash\Doorman\Exceptions\MaxUsesReached;
9
use Clarkeash\Doorman\Exceptions\NotYourInviteCode;
10
use Clarkeash\Doorman\Models\Invite;
11
use Illuminate\Database\Eloquent\ModelNotFoundException;
12
use Illuminate\Support\Str;
13
14
class Manager
15
{
16
    /**
17
     * @param             $code
18
     * @param string|null $email
19
     */
20
    public function redeem($code, string $email = null)
21
    {
22
        $invite = $this->lookupInvite($code);
23
24
        $this->validateInvite($invite, $email);
25
26
        $invite->increment('uses');
0 ignored issues
show
Bug introduced by
The method increment() cannot be called from this context as it is declared protected in class Illuminate\Database\Eloquent\Model.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
27
    }
28
29
    /**
30
     * @param             $code
31
     * @param string|null $email
32
     *
33
     * @return bool
34
     */
35
    public function check($code, string $email = null)
36
    {
37
        try {
38
            $invite = $this->lookupInvite($code);
39
            $this->validateInvite($invite, $email);
40
41
            return true;
42
        } catch (DoormanException $e) {
43
            return false;
44
        }
45
    }
46
47
    /**
48
     * @param $code
49
     *
50
     * @return \Clarkeash\Doorman\Models\Invite
51
     * @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode
52
     */
53
    protected function lookupInvite($code): Invite
54
    {
55
        try {
56
            return Invite::where('code', '=', Str::upper($code))->firstOrFail();
57
        } catch (ModelNotFoundException $e) {
58
            throw new InvalidInviteCode;
59
        }
60
    }
61
62
    /**
63
     * @param \Clarkeash\Doorman\Models\Invite $invite
64
     * @param string|null                      $email
65
     *
66
     * @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode
67
     * @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached
68
     * @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode
69
     */
70
    protected function validateInvite(Invite $invite, string $email = null)
71
    {
72
        if ($invite->max != 0 && $invite->uses >= $invite->max) {
73
            throw new MaxUsesReached;
74
        }
75
76
        if (!is_null($invite->valid_until) && $invite->valid_until->isPast()) {
77
            throw new ExpiredInviteCode;
78
        }
79
80
        if (!is_null($invite->for) && $invite->for != $email) {
81
            throw new NotYourInviteCode;
82
        }
83
    }
84
}
85