Invoice::boot()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 0
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 2
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace NeptuneSoftware\Invoice\Models;
4
5
use Illuminate\Support\Str;
6
use NeptuneSoftware\Invoice\InvoiceReferenceGenerator;
7
use NeptuneSoftware\Invoice\Scopes\InvoiceScope;
8
9
class Invoice extends BaseModel
10
{
11
12
    /**
13
     * Invoice constructor.
14
     * @param array $attributes
15
     */
16 24
    public function __construct(array $attributes = [])
17
    {
18 24
        parent::__construct($attributes);
19
20 24
        $this->setTable(config('invoice.table_names.invoices'));
21 24
    }
22
23
    /**
24
     * The "booting" method of the model.
25
     *
26
     * @return void
27
     */
28 24
    protected static function boot()
29
    {
30 24
        parent::boot();
31 24
        static::addGlobalScope(new InvoiceScope());
32
        static::creating(function ($model) {
33
            /**
34
             * @var \Illuminate\Database\Eloquent\Model $model
35
             */
36 23
            if (!$model->getKey()) {
37 23
                $model->{$model->getKeyName()} = Str::uuid()->toString();
38
            }
39
40 23
            $model->total     = 0;
41 23
            $model->tax       = 0;
42 23
            $model->discount  = 0;
43 23
            $model->is_bill   = false;
44 23
            $model->currency  = config('invoice.default_currency', 'TRY');
45 23
            $model->status    = config('invoice.default_status', 'concept');
46 23
            $model->reference = InvoiceReferenceGenerator::generate();
47 24
        });
48 24
    }
49
}
50