<?php

namespace App\Modules\Tour\Application\Find;

use App\Modules\Tour\Domain\ExistsTour;
use App\Modules\Tour\Domain\IdTour;
use App\Modules\Tour\Domain\TourRepository;
use App\Modules\Shared\Domain\Transaction;

final class TourFinder
{
    public function __construct(
        private TourRepository $repository,
        private Transaction $transaction
    ) {}

    public function __invoke(IdTour $idTour, bool $avoidTransaction = false, bool $lockInShare = false)
    {
        if ($avoidTransaction) {
            $this->transaction->begin();
        }
        $index = $this->repository->find(new ExistsTour($idTour, $lockInShare));
        if ($avoidTransaction) {
            $this->transaction->commit();
        }
        return $index;
    }
}
