Passed
Push — develop ( 9324e6...f3eb57 )
by Septianata
04:31
created

Order   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 23.08%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 25
c 1
b 0
f 0
dl 0
loc 71
ccs 3
cts 13
cp 0.2308
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findOrCreateFromCode() 0 18 2
A generateCode() 0 5 1
1
<?php
2
3
namespace App\Models;
4
5
use App\Enum\OrderStatus as EnumOrderStatus;
6
use App\Infrastructure\Database\Eloquent\Model;
7
use App\Models\OrderStatus as ModelOrderStatus;
8
use Illuminate\Database\Eloquent\Factories\HasFactory;
9
use Illuminate\Support\Carbon;
10
use Illuminate\Support\Str;
11
12
class Order extends Model
13
{
14
    use HasFactory,
0 ignored issues
show
Bug introduced by
The trait App\Models\Concerns\Order\Event requires the property $map which is not provided by App\Models\Order.
Loading history...
introduced by
The trait App\Models\Concerns\Order\Attribute requires some properties which are not provided by App\Models\Order: $bundle_quantity, $total, $sum
Loading history...
15
        Concerns\Order\Attribute,
16
        Concerns\Order\Event,
17
        Concerns\Order\Relation;
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    protected $fillable = [
23
        'code',
24
        'schedule_date',
25
    ];
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    protected $casts = [
31
        'schedule_date' => 'datetime',
32
    ];
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    protected $appends = [
38
        'status',
39
        'item_total_bundle_quantity',
40
        'item_total',
41
    ];
42
43
    /**
44
     * Generate code for this model.
45
     *
46
     * @param  string  $prefix
47
     * @param  \Illuminate\Support\Carbon|null  $date
48
     * @return string
49
     */
50 3
    public function generateCode(string $prefix = 'BCA', Carbon $date = null): string
51
    {
52 3
        $date ??= Carbon::today();
53
54 3
        return $prefix . '-' . $date->format('Ymd') . '-' . Str::random(5);
55
    }
56
57
    /**
58
     * Find the specified order based on the given code or create a new one.
59
     *
60
     * @param  string|null  $code
61
     * @param  \App\Models\Customer  $customer
62
     * @param  callable|null  $callable
63
     * @return static
64
     */
65
    public static function findOrCreateFromCode(?string $code, Customer $customer, callable $callable = null)
66
    {
67
        return static::where('code', $code)->firstOr(function () use ($customer, $callable) {
68
            $order = new Order;
69
70
            $order->setCustomerRelationValue($customer)->save();
71
72
            $orderStatus = new ModelOrderStatus(['status' => EnumOrderStatus::draft()]);
73
74
            $order->statuses()->save(
75
                $orderStatus->setIssuerableRelationValue($customer)
76
            );
77
78
            if ($callable) {
79
                $callable($order);
80
            }
81
82
            return $order;
83
        });
84
    }
85
}
86