Simple problems require simple solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

29 lines
1013 B

<?php
namespace App\Http\Controllers;
use App\Enums\DurationModifier;
use App\Http\Requests\CalculateDurationRequest;
use App\Http\Resources\CalculateDurationResponse;
use App\Services\CalculateDuration;
use DateTimeImmutable;
class CalculateDurationController extends Controller
{
public function __invoke(CalculateDurationRequest $request): CalculateDurationResponse
{
$startData = $request->validated('start');
$endData = $request->validated('end');
$start = DateTimeImmutable::createFromFormat('Y-m-d', $startData['date'], $startData['timezone'] ?? null);
$end = DateTimeImmutable::createFromFormat('Y-m-d', $endData['date'], $endData['timezone'] ?? null);
$calculateDuration = new CalculateDuration($start, $end);
$durationModifier = DurationModifier::tryFrom($request->validated('convert_to')) ?? DurationModifier::None;
return new CalculateDurationResponse(
$calculateDuration->result($durationModifier)
);
}
}