Invite::scopeValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace mathewparet\LaravelInvites\Models;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Invite extends Model
9
{
10
    protected $fillable = [
11
        'email', 'allowed_count', 'valid_upto', 'valid_from'
12
    ];
13
14
    protected $dates = ['valid_from', 'valid_upto'];
15
16
    public function setEmailAttribute($value)
17
    {
18
        $this->attributes['email'] = strtolower($value);
19
    }
20
21
    public function __construct(array $attributes = [])
22
    {
23
24
        $this->code = Str::uuid();
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on mathewparet\LaravelInvites\Models\Invite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
25
        $this->valid_from = now();
0 ignored issues
show
Bug introduced by
The property valid_from does not seem to exist on mathewparet\LaravelInvites\Models\Invite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
26
        $this->allowed_count = 1;
0 ignored issues
show
Bug introduced by
The property allowed_count does not seem to exist on mathewparet\LaravelInvites\Models\Invite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
27
28
        $this->valid_upto = $this->setDefaultExpiry();
0 ignored issues
show
Bug introduced by
The property valid_upto does not seem to exist on mathewparet\LaravelInvites\Models\Invite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
29
30
        $this->table = config('laravelinvites.table');
31
32
        parent::__construct($attributes);
33
34
    }
35
36
    public function redeem()
37
    {
38
        $this->increment('used_count');
39
40
        if ($this->used_count >= $this->allowed_count && config('laravelinvites.delete_on_full', true)) {
0 ignored issues
show
Bug introduced by
The property used_count does not seem to exist on mathewparet\LaravelInvites\Models\Invite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
41
                    $this->delete();
42
        }
43
    }
44
45
    public function scopeValid($query)
46
    {
47
        return $query->where('valid_from', '<=', now())
48
            ->where('valid_upto', '>=', now())
49
            ->whereRaw('allowed_count > used_count');
50
    }
51
52
    public function scopeUseless($query)
53
    {
54
        return $query->where('valid_upto', '<', now())
55
            ->orWhereRaw('allowed_count <= used_count');
56
    }
57
58
    /**
59
     * Set default expiry as per configuration
60
     */
61
    private function setDefaultExpiry()
62
    {
63
        if (config('laravelinvites.expiry.type') === 'none') {
64
                    return null;
65
        }
66
        
67
        if (config('laravelinvites.expiry.type') === 'hours') {
68
                    return now()->addHours(config('laravelinvites.expiry.value'));
69
        } elseif (config('laravelinvites.expiry.type') === "days") {
70
                    return now()->addDays(config('laravelinvites.expiry.days'));
71
        }
72
    }
73
}