58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Database\Seeders;
|
||
|
|
|
||
|
|
use App\Models\Planner;
|
||
|
|
use App\Models\User;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Carbon\CarbonPeriod;
|
||
|
|
use DishPlanner\Schedule\Repositories\ScheduleRepository;
|
||
|
|
use DishPlanner\ScheduledUserDish\Actions\CreateScheduledUserDishAction;
|
||
|
|
use Illuminate\Database\Seeder;
|
||
|
|
|
||
|
|
class ScheduleSeeder extends Seeder
|
||
|
|
{
|
||
|
|
public function run(): void
|
||
|
|
{
|
||
|
|
$this->upcoming();
|
||
|
|
$this->history();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function upcoming(): void
|
||
|
|
{
|
||
|
|
$start = Carbon::now()->startOfDay();
|
||
|
|
$end = $start->copy()->addDays(14);
|
||
|
|
|
||
|
|
$period = CarbonPeriod::create($start, '1 day', $end);
|
||
|
|
$this->createScheduleForPeriod($period);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function history(): void
|
||
|
|
{
|
||
|
|
$end = Carbon::now()->subDay()->startOfDay();
|
||
|
|
$start = $end->copy()->subDays(14);
|
||
|
|
|
||
|
|
$period = CarbonPeriod::create($start, '1 day', $end);
|
||
|
|
$this->createScheduleForPeriod($period);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function createScheduleForPeriod(CarbonPeriod $period): void
|
||
|
|
{
|
||
|
|
$planner = Planner::all()->first() ?? Planner::factory()->create();
|
||
|
|
|
||
|
|
collect($period)
|
||
|
|
->each(fn (Carbon $date) =>
|
||
|
|
User::query()
|
||
|
|
->inRandomOrder()
|
||
|
|
->get()
|
||
|
|
->each(fn (User $user) => (new CreateScheduledUserDishAction())
|
||
|
|
->execute(
|
||
|
|
planner: $planner,
|
||
|
|
schedule: resolve(ScheduleRepository::class)->findOrCreate($planner, $date),
|
||
|
|
userDish: $user->userDishes->random(),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|