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

Generator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 109
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A times() 0 6 1
A uses() 0 6 1
A for() 0 6 1
A expiresOn() 0 6 1
A expiresIn() 0 6 1
A build() 0 10 1
A make() 0 14 2
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();
0 ignored issues
show
Documentation Bug introduced by
The method code does not exist on object<Clarkeash\Doorman\DoormanManager>? 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...
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