. */ namespace SP\Services\Track; use Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\InvalidArgumentException; use SP\Core\Exceptions\QueryException; use SP\DataModel\ItemSearchData; use SP\DataModel\TrackData; use SP\Http\Request; use SP\Repositories\NoSuchItemException; use SP\Repositories\Track\TrackRepository; use SP\Repositories\Track\TrackRequest; use SP\Services\Service; use SP\Services\ServiceException; use SP\Storage\Database\QueryResult; /** * Class TrackService * * @package SP\Services */ final class TrackService extends Service { /** * Tiempo para contador de intentos */ public const TIME_TRACKING = 600; public const TIME_TRACKING_MAX_ATTEMPTS = 10; public const TIME_SLEEP = 0.5; protected ?TrackRepository $trackRepository = null; protected ?Request $request = null; /** * @throws InvalidArgumentException */ public function getTrackRequest(string $source): TrackRequest { $trackRequest = new TrackRequest( time() - self::TIME_TRACKING, $source ); $trackRequest->setTrackIp($this->request->getClientAddress()); return $trackRequest; } /** * @throws QueryException * @throws ConstraintException * @throws NoSuchItemException */ public function delete(int $id): void { if ($this->trackRepository->delete($id) === 0) { throw new NoSuchItemException(__u('Track not found')); } } /** * @throws QueryException * @throws ConstraintException * @throws NoSuchItemException */ public function unlock(int $id): void { if ($this->trackRepository->unlock($id) === 0) { throw new NoSuchItemException(__u('Track not found')); } } /** * @throws ConstraintException * @throws QueryException */ public function clear(): bool { return $this->trackRepository->clear(); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function getById(int $id): TrackData { $result = $this->trackRepository->getById($id); if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('Track not found')); } return $result->getData(); } /** * @return TrackData[] * @throws ConstraintException * @throws QueryException */ public function getAll(): array { return $this->trackRepository->getAll()->getDataAsArray(); } /** * Comprobar los intentos de login * * @return bool True if delay is performed, false otherwise * @throws Exception */ public function checkTracking(TrackRequest $trackRequest): bool { try { $attempts = $this->getTracksForClientFromTime($trackRequest); if ($attempts >= self::TIME_TRACKING_MAX_ATTEMPTS) { $delaySeconds = self::TIME_SLEEP * $attempts; $this->eventDispatcher->notifyEvent('track.delay', new Event($this, EventMessage::factory() ->addDescription(sprintf(__('Attempts exceeded (%d/%d)'), $attempts, self::TIME_TRACKING_MAX_ATTEMPTS)) ->addDetail(__u('Seconds'), $delaySeconds)) ); logger('Tracking delay: ' . $delaySeconds . 's'); sleep($delaySeconds); return true; } } catch (Exception $e) { processException($e); throw $e; } return false; } /** * Devuelve los tracks de un cliente desde un tiempo y origen determinados * * @throws ConstraintException * @throws QueryException */ public function getTracksForClientFromTime(TrackRequest $trackRequest): int { return $this->trackRepository ->getTracksForClientFromTime($trackRequest) ->getNumRows(); } /** * @throws ServiceException * @throws ConstraintException * @throws QueryException */ public function add(TrackRequest $trackRequest): int { if ($trackRequest->getIpv4() === null && $trackRequest->getIpv6() === null ) { throw new ServiceException(__u('IP address not set')); } $result = $this->trackRepository->add($trackRequest); $this->eventDispatcher->notifyEvent( 'track.add', new Event($this, EventMessage::factory() ->addDescription($this->request->getClientAddress(true))) ); return $result; } /** * @throws ConstraintException * @throws QueryException */ public function search(ItemSearchData $itemSearchData): QueryResult { return $this->trackRepository->search($itemSearchData); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function initialize(): void { $this->trackRepository = $this->dic->get(TrackRepository::class); $this->request = $this->dic->get(Request::class); } }