Completed
Push — master ( d3f292...6a180a )
by Ashley
05:22
created

Generator::expiresOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Clarkeash\Doorman;
4
5
use Carbon\Carbon;
6
use Clarkeash\Doorman\Exceptions\DuplicateException;
7
use Clarkeash\Doorman\Models\Invite;
8
9
class Generator
10
{
11
    protected $amount = 1;
12
    protected $uses = 1;
13
    protected $email = null;
14
    protected $expiry;
15
16
    /**
17
     * @var \Clarkeash\Doorman\DoormanManager
18
     */
19
    protected $manager;
20
21 8
    public function __construct(DoormanManager $manager)
22
    {
23 8
        $this->manager = $manager;
24 8
    }
25
26
    /**
27
     * @param int $amount
28
     *
29
     * @return $this
30
     */
31 2
    public function times(int $amount = 1)
32
    {
33 2
        $this->amount = $amount;
34
35 2
        return $this;
36
    }
37
38
    /**
39
     * @param int $amount
40
     *
41
     * @return $this
42
     */
43 1
    public function uses(int $amount = 1)
44
    {
45 1
        $this->uses = $amount;
46
47 1
        return $this;
48
    }
49
50
    /**
51
     * @param string $email
52
     *
53
     * @return $this
54
     * @throws \Clarkeash\Doorman\Exceptions\DuplicateException
55
     */
56 3
    public function for(string $email)
57
    {
58 3
        if (Invite::where('for', strtolower($email))->first()) {
59 1
            throw new DuplicateException('You cannot create more than 1 invite code for an email');
60
        }
61
62 3
        $this->email = $email;
63
64 3
        return $this;
65
    }
66
67
    /**
68
     * @param \Carbon\Carbon $date
69
     *
70
     * @return $this
71
     */
72
    public function expiresOn(Carbon $date)
73
    {
74
        $this->expiry = $date;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param int $days
81
     *
82
     * @return $this
83
     */
84
    public function expiresIn($days = 14)
85
    {
86
        $this->expiry = Carbon::now(config('app.timezone'))->addDays($days)->endOfDay();
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return \Clarkeash\Doorman\Models\Invite
93
     */
94 6
    protected function build(): Invite
95
    {
96 6
        $invite = new Invite;
97 6
        $invite->code = $this->manager->code();
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<Clarkeash\Doorman\Models\Invite>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98 6
        $invite->for = $this->email;
0 ignored issues
show
Documentation introduced by
The property for does not exist on object<Clarkeash\Doorman\Models\Invite>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99 6
        $invite->max = $this->uses;
0 ignored issues
show
Documentation introduced by
The property max does not exist on object<Clarkeash\Doorman\Models\Invite>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100 6
        $invite->valid_until = $this->expiry;
0 ignored issues
show
Documentation introduced by
The property valid_until does not exist on object<Clarkeash\Doorman\Models\Invite>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
102 6
        return $invite;
103
    }
104
105
    /**
106
     * @return \Illuminate\Support\Collection
107
     */
108 7
    public function make()
109
    {
110 7
        $invites = collect();
111
112 7
        if (!is_null($this->email) && $this->amount > 1) {
113 1
            throw new DuplicateException('You cannot create more than 1 invite code for an email');
114
        }
115
116 6
        for ($i = 0; $i < $this->amount; $i++) {
117 6
            $invite = $this->build();
118
119 6
            $invites->push($invite);
120
121 6
            $invite->save();
122
        }
123
124 6
        return $invites;
125
    }
126
}
127