ldc-supportweb/database/factories/WeekPlanningFactory.php

77 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace Database\Factories;
use App\Enums\Planning\CellModel;
use App\Enums\Planning\CellType;
use App\Models\Project;
use App\Models\Site;
use App\Models\Ticket;
use App\Models\WeekPlanning;
use Illuminate\Database\Eloquent\Factories\Factory;
use ReflectionFunction;
class WeekPlanningFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = WeekPlanning::class;
/** @var \Illuminate\Support\Carbon */
private static $currentDate = null;
private static $user_id = null;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$func = new ReflectionFunction(collect($this->states)->last());
if (static::$user_id and $func->getStaticVariables()['state']['user_id'] != static::$user_id) {
static::$currentDate = null;
}
if (!static::$currentDate) {
static::$user_id = $func->getStaticVariables()['state']['user_id'];
static::$currentDate = $func->getStaticVariables()['state']['start'];
} else {
static::$currentDate = static::$currentDate->endOfWeek()->addDay();
}
$wBlock = WeekPlanning::getWeekBlock(static::$currentDate);
foreach ($wBlock as &$dayRows) {
foreach ($dayRows as &$cell) {
$cell['type'] = collect(CellType::toArray())->random()->value;
if (rand(0, 3)) {
$cellModel = collect(CellModel::toArray())->random();
$cell['cellModel'] = $cellModel->value;
switch ($cellModel) {
case (new Ticket())->getTable():
$cell['foreign'] = Ticket::select(['id'])
->inRandomOrder()->first()->id;
break;
case (new Site())->getTable():
$cell['foreign'] = Site::select(['id'])
->inRandomOrder()->first()->id;
break;
case (new Project())->getTable():
$cell['foreign'] = Project::select(['id'])
->inRandomOrder()->first()->id;
break;
}
$cell['text'] = $this->faker->text(rand(20, 50));
}
}
}
return [
'start' => static::$currentDate,
'planning' => $wBlock
];
}
}