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

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Enums\DurationModifier;
  4. use App\Http\Requests\CalculateDurationRequest;
  5. use App\Http\Resources\CalculateDurationResponse;
  6. use App\Services\CalculateDuration;
  7. use DateTimeImmutable;
  8. class CalculateDurationController extends Controller
  9. {
  10. public function __invoke(CalculateDurationRequest $request): CalculateDurationResponse
  11. {
  12. $startData = $request->validated('start');
  13. $endData = $request->validated('end');
  14. $start = DateTimeImmutable::createFromFormat('Y-m-d', $startData['date'], $startData['timezone'] ?? null);
  15. $end = DateTimeImmutable::createFromFormat('Y-m-d', $endData['date'], $endData['timezone'] ?? null);
  16. $calculateDuration = new CalculateDuration($start, $end);
  17. $durationModifier = DurationModifier::tryFrom($request->validated('convert_to')) ?? DurationModifier::None;
  18. return new CalculateDurationResponse(
  19. $calculateDuration->result($durationModifier)
  20. );
  21. }
  22. }