diff --git a/src/PersistentData/Model/Place.php b/src/PersistentData/Model/Place.php new file mode 100644 index 0000000..d8d26b1 --- /dev/null +++ b/src/PersistentData/Model/Place.php @@ -0,0 +1,150 @@ + Map::class]; + + private ?Map $map = null; + + private ?int $mapId = null; + + private Position $position; + + private ?string $panoIdCached = null; + + private ?DateTime $panoIdCachedTimestamp = null; + + public function __construct() + { + $this->position = new Position(0.0, 0.0); + } + + public function setMap(Map $map): void + { + $this->map = $map; + } + + public function setMapId(int $mapId): void + { + $this->mapId = $mapId; + } + + public function setPosition(Position $position): void + { + $this->position = $position; + } + + public function setLat(float $lat): void + { + $this->position->setLat($lat); + } + + public function setLng(float $lng): void + { + $this->position->setLng($lng); + } + + public function setPanoIdCached(?string $panoIdCached): void + { + $this->panoIdCached = $panoIdCached; + } + + public function setPanoIdCachedTimestampDate(?DateTime $panoIdCachedTimestamp): void + { + $this->panoIdCachedTimestamp = $panoIdCachedTimestamp; + } + + public function setPanoIdCachedTimestamp(?string $panoIdCachedTimestamp): void + { + if ($panoIdCachedTimestamp !== null) { + $this->panoIdCachedTimestamp = new DateTime($panoIdCachedTimestamp); + } + } + + public function getMap(): ?Map + { + return $this->map; + } + + public function getMapId(): ?int + { + return $this->mapId; + } + + public function getPosition(): Position + { + return $this->position; + } + + public function getLat(): float + { + return $this->position->getLat(); + } + + public function getLng(): float + { + return $this->position->getLng(); + } + + public function getFreshPanoId(bool $canBeIndoor = false): ?string + { + if ( + $this->panoIdCachedTimestamp !== null && + (clone $this->panoIdCachedTimestamp)->add(new DateInterval('P1D')) > new DateTime() + ) { + return $this->panoIdCached; + } + + $request = new Request('https://maps.googleapis.com/maps/api/streetview/metadata', Request::HTTP_GET); + $request->setQuery([ + 'key' => $_ENV['GOOGLE_MAPS_SERVER_API_KEY'], + 'location' => $this->position->getLat() . ',' . $this->position->getLng(), + 'source' => $canBeIndoor ? 'default' : 'outdoor' + ]); + + $response = $request->send(); + $panoData = json_decode($response->getBody(), true); + $panoId = $panoData['status'] === 'OK' ? $panoData['pano_id'] : null; + + // enable indoor panos if no outdoor found + if ($panoId === null && !$canBeIndoor) { + return $this->getFreshPanoId(true); + } + + $this->panoIdCached = $panoId; + $this->panoIdCachedTimestamp = new DateTime('now'); + + (new PersistentDataManager())->saveToDb($this); + + return $panoId; + } + + public function getPanoIdCached(): ?string + { + return $this->panoIdCached; + } + + public function getPanoIdCachedTimestampDate(): ?DateTime + { + return $this->panoIdCachedTimestamp; + } + + public function getPanoIdCachedTimestamp(): ?string + { + if ($this->panoIdCachedTimestamp === null) { + return null; + } + + return $this->panoIdCachedTimestamp->format('Y-m-d H:i:s'); + } +} diff --git a/src/Util/Geo/Position.php b/src/Util/Geo/Position.php index 33eb0cd..bdd89ca 100644 --- a/src/Util/Geo/Position.php +++ b/src/Util/Geo/Position.php @@ -13,6 +13,16 @@ class Position $this->lng = $lng; } + public function setLat(float $lat): void + { + $this->lat = $lat; + } + + public function setLng(float $lng): void + { + $this->lng = $lng; + } + public function getLat(): float { return $this->lat;