-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServeOrder.php
73 lines (62 loc) · 1.91 KB
/
ServeOrder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace App\Jobs;
use App\Models\Dish;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class ServeOrder implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private Order $order;
/**
* Create a new job instance.
*/
public function __construct()
{
// берем 1 заказ из пулла заказов
$this->order = Order::where('status', 'created')->first();
}
/**
* Execute the job.
*/
public function handle(): void
{
Log::info("Order {$this->order->id} is waiting the chef");
$this->order->status = 'waiting';
$this->order->save();
sleep(1);
Log::info("Order {$this->order->id} is being proceeded");
$this->order->status = 'working';
$this->order->save();
sleep(1.5);
// rand() статус, если canceled - возвращаем неиспользуемые блюда на склад
if (rand(0, 99) < 33) {
Log::info("Order {$this->order->id} is canceled");
$this->freeDishes($this->order->dishes);
$this->order->status = 'canceled';
} else {
Log::info("Order {$this->order->id} is finished");
$this->order->status = 'finished';
}
$this->order->save();
}
/**
* @param $dishes
* @return void
*/
private function freeDishes($dishes): void
{
foreach ($dishes as $record) {
$pivotData = $record->pivot;
$dish = Dish::find($record->id);
if ($dish) {
$dish->quantity += $pivotData->quantity;
$dish->save();
}
}
}
}