RedeemCode::setRedeemed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Furic\RedeemCodes\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class RedeemCode extends Model
8
{
9
10
    protected $fillable = ['event_id', 'code', 'redeemed', 'reusable'];
11
    protected $hidden = ['created_at', 'updated_at'];
12
    protected $appends = ['rewards'];
13
    protected $casts = ['redeemed' => 'boolean', 'reusable' => 'boolean'];
14
15
    public static function findByCode($code)
16
    {
17
        return SELF::where('code', $code)->first();
18
    }
19
20
    public function event()
21
    {
22
        return $this->belongsTo('Furic\RedeemCodes\Models\Event');
23
    }
24
    
25
    public function rewards()
26
    {
27
        if ($this->event != null) {
28
            return $this->event->hasMany('Furic\RedeemCodes\Models\RedeemCodeReward');
29
        } else {
30
            return $this->hasMany('Furic\RedeemCodes\Models\RedeemCodeReward');
31
        }
32
    }
33
    
34
    public function getRewardsAttribute()
35
    {
36
        return $this->rewards()->get();
37
    }
38
39
    public function setRedeemed()
40
    {
41
        $this->redeemed = true;
42
        $this->save();
43
    }
44
45
}
46