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

Manager::check()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
nop 2
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