Invite   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 63
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A setEmailAttribute() 0 3 1
A redeem() 0 6 3
A setDefaultExpiry() 0 10 4
A scopeValid() 0 5 1
A scopeUseless() 0 4 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
}