Passed
Push — master ( 146b61...80e19c )
by Mathew
05:37
created

Invite   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultExpiry() 0 10 4
A scopeValid() 0 5 1
A scopeUseless() 0 4 1
A __construct() 0 12 1
A setEmailAttribute() 0 3 1
A redeem() 0 6 3
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
    public function scopeValid($query)
45
    {
46
        return $query->where('valid_from','<=', now())
47
            ->where('valid_upto', '>=', now())
48
            ->whereRaw('allowed_count > used_count');
49
    }
50
51
    public function scopeUseless($query)
52
    {
53
        return $query->where('valid_upto', '<', now())
54
            ->orWhereRaw('allowed_count <= used_count');
55
    }
56
57
    /**
58
     * Set default expiry as per configuration
59
     */
60
    private function setDefaultExpiry()
61
    {
62
        if(config('laravelinvites.expiry.type')==='none')
63
            return null;
64
        
65
        if(config('laravelinvites.expiry.type') === 'hours')
66
            return now()->addHours(config('laravelinvites.expiry.value'));
67
            
68
        elseif(config('laravelinvites.expiry.type') === "days")
69
            return now()->addDays(config('laravelinvites.expiry.days'));
70
    }
71
}