Completed
Pull Request — master (#23)
by Ashley
01:57
created

Doorman   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A redeem() 0 6 1
A check() 0 10 2
A prep() 0 8 1
A lookupInvite() 0 8 2
B validateInvite() 0 14 5
A generate() 0 4 1
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 Doorman
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');
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
            $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->isFull()) {
81
            throw new MaxUsesReached(trans('doorman::messages.maxed', [ 'code' => $invite->code ]));
82
        }
83
84
        if ($invite->hasExpired()) {
85
            throw new ExpiredInviteCode(trans('doorman::messages.expired', [ 'code' => $invite->code ]));
86
        }
87
88
        if ($invite->isRestricted() && !$invite->isRestrictedFor($email)) {
89
            throw new NotYourInviteCode(trans('doorman::messages.restricted', [ 'code' => $invite->code ]));
90
        }
91
    }
92
93
    public function generate()
94
    {
95
        return app(Generator::class);
96
    }
97
}
98