Files
points-of-interest/server/src/Service/PointProximityValidator.php
T
2025-10-10 16:13:48 +02:00

41 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use App\ValueObject\Distance;
use App\ValueObject\Point;
use App\Exception\PointTooFarException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final class PointProximityValidator
{
public function __construct(
#[Autowire('%app.max_signal_distance_km%')]
private readonly float $maximumDistanceKm,
#[Autowire(service: 'monolog.logger.signals')]
private readonly LoggerInterface $logger,
) {
}
public function assertWithinRange(Point $userLocation, Point $signalLocation): void
{
$distance = Distance::betweenPoints($userLocation, $signalLocation)->inKilometers();
$this->logger->debug('Calculated proximity between user and signal.', [
'distance_km' => $distance,
'max_distance_km' => $this->maximumDistanceKm,
]);
if ($distance > $this->maximumDistanceKm) {
$this->logger->warning('Rejected signal because it is beyond the allowed proximity.', [
'distance_km' => $distance,
'max_distance_km' => $this->maximumDistanceKm,
]);
throw new PointTooFarException($this->maximumDistanceKm);
}
}
}