Passed
Push — main ( cee2a9...63dbdd )
by Richard
04:42
created

RedeemCode::rewards()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
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 $visible = ['event_id', 'code', 'reusable', 'rewards'];
12
    protected $appends = ['rewards'];
13
14
    public static function findByCode($code)
15
    {
16
        return SELF::where('code', $code)->first();
17
    }
18
19
    public function event()
20
    {
21
        return $this->belongsTo('Furic\RedeemCodes\Event');
22
    }
23
    
24
    public function rewards()
25
    {
26
        if ($this->event != null) {
27
            return $this->event->hasMany('Furic\RedeemCodes\RedeemCodeReward');
28
        } else {
29
            return $this->hasMany('Furic\RedeemCodes\RedeemCodeReward');
30
        }
31
    }
32
    
33
    public function getRewardsAttribute()
34
    {
35
        return $this->rewards()->get();
36
    }
37
38
    public function setRedeemed()
39
    {
40
        $this->redeemed = true;
41
        $this->save();
42
    }
43
44
}
45