Passed
Push — master ( a8b594...f799ef )
by
unknown
02:38 queued 11s
created

InvoiceLine::boot()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 0
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NeptuneSoftware\Invoice\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
8
class InvoiceLine extends Model
9
{
10
    protected $guarded = [];
11
12
    public $incrementing = false;
13
14
    protected $fillable = [
15
        'amount', 'tax', 'tax_details', 'invoice_id', 'description', 'invoiceable_id',
16
        'invoiceable_type', 'name', 'discount', 'quantity', 'is_free', 'is_complimentary'
17
    ];
18
19
    protected $casts = [
20
        'tax_details' => 'array'
21
    ];
22
23
    /**
24
     * InvoiceLine constructor.
25
     * @param array $attributes
26
     */
27 36
    public function __construct(array $attributes = [])
28
    {
29 36
        parent::__construct($attributes);
30
31 36
        $this->setTable(config('invoice.table_names.invoice_lines'));
32 36
    }
33
34 36
    protected static function boot()
35
    {
36 36
        parent::boot();
37
        static::creating(function ($model) {
38
            /**
39
             * @var \Illuminate\Database\Eloquent\Model $model
40
             */
41 36
            if (!$model->getKey()) {
42 36
                $model->{$model->getKeyName()} = Str::uuid()->toString();
43
            }
44 36
        });
45 36
    }
46
47
    public function invoice()
48
    {
49
        return $this->belongsTo(Invoice::class);
50
    }
51
52
    public function bill()
53
    {
54
        return $this->belongsTo(Bill::class, 'invoice_id');
55
    }
56
57
    /**
58
     * Get the owning invoiceable model.
59
     */
60
    public function invoiceable()
61
    {
62
        return $this->morphTo();
63
    }
64
}
65