Completed
Push — master ( 3f8137...024a5e )
by Ashley
01:53
created

Manager::validateInvite()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 7
nc 4
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
    public $error = '';
17
18
    /**
19
     * @param             $code
20
     * @param string|null $email
21
     */
22
    public function redeem($code, string $email = null)
23
    {
24
        $invite = $this->prep($code, $email);
25
26
        $invite->increment('uses');
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
            $this->prep($code, $email);
39
            return true;
40
        } catch (DoormanException $e) {
41
            $this->error = $e->getMessage();
42
            return false;
43
        }
44
    }
45
46
    protected function prep($code, string $email = null)
47
    {
48
        $this->error = '';
49
        $invite = $this->lookupInvite($code);
50
        $this->validateInvite($invite, $email);
51
52
        return $invite;
53
    }
54
55
    /**
56
     * @param $code
57
     *
58
     * @return \Clarkeash\Doorman\Models\Invite
59
     * @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode
60
     */
61
    protected function lookupInvite($code): Invite
62
    {
63
        try {
64
            return Invite::where('code', '=', Str::upper($code))->firstOrFail();
65
        } catch (ModelNotFoundException $e) {
66
            throw new InvalidInviteCode(trans('doorman::messages.invalid', [ 'code' => $code ]));
67
        }
68
    }
69
70
    /**
71
     * @param \Clarkeash\Doorman\Models\Invite $invite
72
     * @param string|null                      $email
73
     *
74
     * @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode
75
     * @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached
76
     * @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode
77
     */
78
    protected function validateInvite(Invite $invite, string $email = null)
79
    {
80
        if ($invite->max != 0 && $invite->uses >= $invite->max) {
81
            throw new MaxUsesReached(trans('doorman::messages.maxed', [ 'code' => $invite->code ]));
82
        }
83
84
        if (!is_null($invite->valid_until) && $invite->valid_until->isPast()) {
85
            throw new ExpiredInviteCode(trans('doorman::messages.expired', [ 'code' => $invite->code ]));
86
        }
87
88
        if (!is_null($invite->for) && $invite->for != $email) {
89
            throw new NotYourInviteCode(trans('doorman::messages.restricted', [ 'code' => $invite->code ]));
90
        }
91
    }
92
}
93