mathewparet /
laravel-invites
| 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
|
|||
| 25 | $this->valid_from = now(); |
||
|
0 ignored issues
–
show
|
|||
| 26 | $this->allowed_count = 1; |
||
|
0 ignored issues
–
show
|
|||
| 27 | |||
| 28 | $this->valid_upto = $this->setDefaultExpiry(); |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 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 | } |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.