Refactor point value object and add observability

This commit is contained in:
Bernard Ngandu
2025-10-10 14:55:36 +02:00
parent 8a43d3967c
commit 68eb54995f
46 changed files with 3691 additions and 229 deletions
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Service;
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 = $this->distanceInKm($userLocation, $signalLocation);
$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);
}
}
private function distanceInKm(Point $a, Point $b): float
{
$lat1 = deg2rad($a->getLat());
$lat2 = deg2rad($b->getLat());
$deltaLat = deg2rad($b->getLat() - $a->getLat());
$deltaLng = deg2rad($b->getLng() - $a->getLng());
$haversine = sin($deltaLat / 2) ** 2 + cos($lat1) * cos($lat2) * sin($deltaLng / 2) ** 2;
$c = 2 * atan2(sqrt($haversine), sqrt(1 - $haversine));
return 6371 * $c;
}
}