Completed
Pull Request — master (#58)
by Ashley
03:09
created

Generator::once()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Clarkeash\Doorman;
4
5
use Carbon\Carbon;
6
use Clarkeash\Doorman\Exceptions\DuplicateException;
7
use Clarkeash\Doorman\Models\BaseInvite;
8
use Illuminate\Support\Str;
9
10
class Generator
11
{
12
    protected $amount = 1;
13
    protected $uses = 1;
14
    protected $email = null;
15
    protected $expiry;
16
17
    /**
18
     * @var \Clarkeash\Doorman\DoormanManager
19
     */
20
    protected $manager;
21
22
    /**
23
     * @var BaseInvite
24
     */
25
    protected $invite;
26
27 16
    public function __construct(DoormanManager $manager, BaseInvite $invite)
28
    {
29 16
        $this->manager = $manager;
30 16
        $this->invite = $invite;
31 16
    }
32
33
    /**
34
     * @param int $amount
35
     *
36
     * @return $this
37
     */
38 2
    public function times(int $amount = 1)
39
    {
40 2
        $this->amount = $amount;
41
42 2
        return $this;
43
    }
44
45
    /**
46
     * @param int $amount
47
     *
48
     * @return $this
49
     */
50 1
    public function uses(int $amount = 1)
51
    {
52 1
        $this->uses = $amount;
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * @param string $email
59
     *
60
     * @return $this
61
     * @throws \Clarkeash\Doorman\Exceptions\DuplicateException
62
     */
63 3
    public function for(string $email)
64
    {
65 3
        if ($this->invite->where('for', strtolower($email))->first()) {
66 1
            throw new DuplicateException('You cannot create more than 1 invite code for an email');
67
        }
68
69 3
        $this->email = $email;
70
71 3
        return $this;
72
    }
73
74
    /**
75
     * @param \DateTimeInterface $date
76
     *
77
     * @return $this
78
     */
79 4
    public function expiresOn(\DateTimeInterface $date)
80
    {
81 4
        $this->expiry = $date;
82
83 4
        return $this;
84
    }
85
86
    /**
87
     * @param int $days
88
     *
89
     * @return $this
90
     */
91 1
    public function expiresIn($days = 14)
92
    {
93 1
        $this->expiry = Carbon::now(config('app.timezone'))->addDays($days)->endOfDay();
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * @return \Clarkeash\Doorman\Models\BaseInvite
100
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
101
     */
102 14
    protected function build(): BaseInvite
103
    {
104 14
        $invite = app()->make(BaseInvite::class);
105 14
        $invite->code = Str::upper($this->manager->code());
106 14
        $invite->for = $this->email;
107 14
        $invite->max = $this->uses;
108 14
        $invite->valid_until = $this->expiry;
109
110 14
        return $invite;
111
    }
112
113
    /**
114
     * @return \Illuminate\Support\Collection
115
     * @throws DuplicateException
116
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
117
     */
118 14
    public function make()
119
    {
120 14
        $invites = collect();
121
122 14
        if (!is_null($this->email) && $this->amount > 1) {
123 1
            throw new DuplicateException('You cannot create more than 1 invite code for an email');
124
        }
125
126 13
        for ($i = 0; $i < $this->amount; $i++) {
127 13
            $invite = $this->build();
128
129 13
            $invites->push($invite);
130
131 13
            $invite->save();
132
        }
133
134 13
        return $invites;
135
    }
136
137
    /**
138
     * @return BaseInvite
139
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
140
     */
141 1
    public function once(): BaseInvite
142
    {
143 1
        return tap($this->build())->save();
144
    }
145
}
146