<?php

namespace App\Modules\Passenger\Application\Find;

use App\Modules\Passenger\Domain\ExistsPassenger;
use App\Modules\Passenger\Domain\IdPassenger;
use App\Modules\Passenger\Domain\PassengerRepository;
use App\Modules\Shared\Domain\Transaction;

final class PassengerFinder
{
    public function __construct(
        private PassengerRepository $repository,
        private Transaction $transaction
    ) {}

    public function __invoke(
        IdPassenger $idPassenger,
        bool $avoidTransaction = false,
        bool $lockInShare = false
    ) {
        $index = $this->transaction->transactional(
            fn() => $this->repository->find(new ExistsPassenger($idPassenger, $lockInShare)),
            $avoidTransaction
        );
        return $index;
    }
}
