Completed
Push — master ( 5feae0...e3746a )
by Ashley
04:09
created

Doorman   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 108
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A redeem() 0 6 1
A check() 0 10 2
A prep() 0 8 1
A lookupInvite() 0 8 2
A 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\BaseInvite;
11
use Illuminate\Database\Eloquent\ModelNotFoundException;
12
use Illuminate\Support\Str;
13
14
class Doorman
15
{
16
    public $error = '';
17
18
    /**
19
     * @var BaseInvite
20
     */
21
    protected $invite;
22
23 34
    public function __construct(BaseInvite $invite)
24
    {
25 34
        $this->invite = $invite;
26 34
    }
27
28
    /**
29
     * @param             $code
30
     * @param string|null $email
31
     * @throws ExpiredInviteCode
32
     * @throws InvalidInviteCode
33
     * @throws MaxUsesReached
34
     * @throws NotYourInviteCode
35
     */
36 10
    public function redeem($code, string $email = null)
37
    {
38 10
        $invite = $this->prep($code, $email);
39
40 6
        $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...
41 6
    }
42
43
    /**
44
     * @param             $code
45
     * @param string|null $email
46
     *
47
     * @return bool
48
     */
49 12
    public function check($code, string $email = null)
50
    {
51
        try {
52 12
            $this->prep($code, $email);
53 7
            return true;
54 5
        } catch (DoormanException $e) {
55 5
            $this->error = $e->getMessage();
56 5
            return false;
57
        }
58
    }
59
60
    /**
61
     * @param             $code
62
     * @param string|null $email
63
     *
64
     * @return \Clarkeash\Doorman\Models\BaseInvite
65
     * @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode
66
     * @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode
67
     * @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached
68
     * @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode
69
     */
70 22
    protected function prep($code, string $email = null)
71
    {
72 22
        $this->error = '';
73 22
        $invite = $this->lookupInvite($code);
74 20
        $this->validateInvite($invite, $email);
75
76 13
        return $invite;
77
    }
78
79
    /**
80
     * @param $code
81
     *
82
     * @return \Clarkeash\Doorman\Models\BaseInvite
83
     * @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode
84
     */
85 22
    protected function lookupInvite($code): BaseInvite
86
    {
87
        try {
88 22
            return $this->invite->where('code', '=', Str::upper($code))->firstOrFail();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Clarkeash\Doorman\Models\BaseInvite>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
89 2
        } catch (ModelNotFoundException $e) {
90 2
            throw new InvalidInviteCode(trans('doorman::messages.invalid', ['code' => $code]));
91
        }
92
    }
93
94
    /**
95
     * @param \Clarkeash\Doorman\Models\BaseInvite $invite
96
     * @param string|null $email
97
     *
98
     * @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode
99
     * @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached
100
     * @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode
101
     */
102 20
    protected function validateInvite(BaseInvite $invite, string $email = null)
103
    {
104 20
        if ($invite->isFull()) {
105 2
            throw new MaxUsesReached(trans('doorman::messages.maxed', ['code' => $invite->code]));
106
        }
107
108 18
        if ($invite->hasExpired()) {
109 2
            throw new ExpiredInviteCode(trans('doorman::messages.expired', ['code' => $invite->code]));
110
        }
111
112 16
        if ($invite->isRestricted() && !$invite->isRestrictedFor($email)) {
113 3
            throw new NotYourInviteCode(trans('doorman::messages.restricted', ['code' => $invite->code]));
114
        }
115 13
    }
116
117 12
    public function generate()
118
    {
119 12
        return app(Generator::class);
120
    }
121
}
122