|
|
- <?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)
- );
- }
- }
|