Files
theme-park-app/app/Http/Services/PhantasialandApi.php

133 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2024-08-20 21:08:23 +02:00
<?php
namespace App\Http\Services;
use App\Data\PointOfInterest;
use App\Models\ThemeParkUser;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
class PhantasialandApi
{
private readonly string $uri;
private readonly PendingRequest $client;
function __construct()
{
$this->uri = 'https://api.phlsys.de/api/';
$this->client = Http::baseUrl($this->uri);
2024-08-20 21:08:23 +02:00
}
public function getPointsOfInterest(): Collection
{
$response = $this->client->get('pois');
$pois = collect($response->json());
// NOTE: Either poiNumber or poiNumberWinter is always null
// TODO: Use a generic field name and fill in the one that is not null
// TODO: Add a type if the poi is winter only
// NOTE: we filter everything out that is not a ATTRACTIONS
// NOTE: we filter everything out that is adminOnly = true
$pois = $pois->filter(function ($poi) {
return $poi['category'] === 'ATTRACTIONS' && $poi['adminOnly'] === false &&
2024-08-21 18:57:36 +02:00
$poi['poiNumber'] !== null &&
$poi["parkMonitorReferenceName"] !== null &&
in_array('SUMMER', $poi['seasons']);
2024-08-20 21:08:23 +02:00
});
// sort by parkMonitorReferenceName
$pois = $pois->sortBy('parkMonitorReferenceName');
return $pois->transform(function ($poi) {
return new PointOfInterest(
id: $poi['id'],
poiNumber: $poi['poiNumber'],
poiNumberWinter: $poi['poiNumberWinter'],
category: $poi['category'],
tags: $poi['tags'],
area: $poi['area'],
seasons: $poi['seasons'],
title: $poi['_title']['de'],
description: $poi['_description']['de'],
entrance: $poi['_entrance']['world'],
titleImage: key_exists('titleImage', $poi) ? $poi['titleImage']['url'] : null,
);
});
}
public function getLiveQueueTimes(): Collection
{
[
'longitude' => $longitude,
'latitude' => $latitude
] = $this->getRandomLocation();
$latitude = number_format($latitude, 14);
$longitude = number_format($longitude, 14);
$user = $this->getThemeParkUser();
$user->authenticate();
2024-08-20 21:08:23 +02:00
$response = $this->client->get('signage-snapshots', [
'loc' => "$latitude,$longitude",
'access_token' => $user->access_token,
2024-08-20 21:08:23 +02:00
]);
return collect($response->json());
}
private function getThemeParkUser(): ThemeParkUser
{
if(ThemeParkUser::query()->count() === 0) {
$userData = $this->createUser();
return ThemeParkUser::query()->create([
'username' => $userData['email'],
'password' => $userData['password'],
]);
}
return ThemeParkUser::query()->first();
}
2024-08-20 21:08:23 +02:00
public function createUser(): array
{
$username = uuid_create().'@android.com';
$password = uuid_create();
$response = $this->client->post('app-users', [
'email' => $username,
'password' => $password,
'language' => 'en',
'platform' => 'android',
]);
return [
...$response->json(),
'password' => $password,
];
}
public function getAccessToken(ThemeParkUser $user): string
{
$response = $this->client->post('app-users/login', [
'email' => $user->username,
'password' => $user->password,
'ttl' => now()->diffInSeconds(now()->addYear()),
]);
return $response->json()['id'];
}
private function getRandomLocation(): array
{
// TODO: Its not random right now
// $rnd = mt_rand() / mt_getrandmax();
return [
'longitude' => 6.878342628,
'latitude' => 50.800659529,
];
}
}