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

Generator::expiresIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Clarkeash\Doorman;
4
5
use Carbon\Carbon;
6
use Clarkeash\Doorman\Models\Invite;
7
use Illuminate\Support\Str;
8
9
class Generator
10
{
11
    protected $amount = 1;
12
    protected $uses = 1;
13
    protected $email;
14
    protected $expiry;
15
16
    /**
17
     * @var \Clarkeash\Doorman\DoormanManager
18
     */
19
    protected $manager;
20
21
    public function __construct(DoormanManager $manager)
22
    {
23
        $this->manager = $manager;
24
    }
25
26
    /**
27
     * @param int $amount
28
     *
29
     * @return $this
30
     */
31
    public function times(int $amount = 1)
32
    {
33
        $this->amount = $amount;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param int $amount
40
     *
41
     * @return $this
42
     */
43
    public function uses(int $amount = 1)
44
    {
45
        $this->uses = $amount;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $email
52
     *
53
     * @return $this
54
     */
55
    public function for (string $email)
56
    {
57
        $this->email = $email;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param \Carbon\Carbon $date
64
     *
65
     * @return $this
66
     */
67
    public function expiresOn(Carbon $date)
68
    {
69
        $this->expiry = $date;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param int $days
76
     *
77
     * @return $this
78
     */
79
    public function expiresIn($days = 14)
80
    {
81
        $this->expiry = Carbon::now(config('app.timezone'))->addDays($days)->endOfDay();
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return \Clarkeash\Doorman\Models\Invite
88
     */
89
    protected function build(): Invite
90
    {
91
        $invite = new Invite;
92
        $invite->code = $this->manager->code();
93
        $invite->for = $this->email;
94
        $invite->max = $this->uses;
95
        $invite->valid_until = $this->expiry;
96
97
        return $invite;
98
    }
99
100
    /**
101
     * @return \Illuminate\Support\Collection
102
     */
103
    public function make()
104
    {
105
        $invites = collect();
106
107
        for ($i = 0; $i < $this->amount; $i++) {
108
            $invite = $this->build();
109
110
            $invites->push($invite);
111
112
            $invite->save();
113
        }
114
115
        return $invites;
116
    }
117
}
118