gps_system/app/Services/MapService.php
sackey 6b878bb0a0
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Initial commit
2025-09-12 16:19:56 +00:00

135 lines
3.8 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Config;
class MapService
{
public function getDefaultProvider(): string
{
$default = Config::get('services.maps.default_provider', 'openstreetmap');
// Handle legacy config value
if ($default === 'leaflet') {
return 'openstreetmap';
}
return $default;
}
public function getAvailableProviders(): array
{
$providers = Config::get('services.maps.providers', []);
// Filter out providers that require API keys but don't have them
return array_filter($providers, function ($provider, $key) {
if (!$provider['requires_api_key']) {
return true;
}
return !empty($provider['api_key']) && ($provider['enabled'] ?? false);
}, ARRAY_FILTER_USE_BOTH);
}
public function getProviderConfig(string $provider): ?array
{
$providers = Config::get('services.maps.providers', []);
return $providers[$provider] ?? null;
}
public function isProviderEnabled(string $provider): bool
{
$config = $this->getProviderConfig($provider);
if (!$config) {
return false;
}
if (!$config['requires_api_key']) {
return true;
}
return !empty($config['api_key']) && ($config['enabled'] ?? false);
}
public function getMapStyles(string $provider): array
{
$config = $this->getProviderConfig($provider);
switch ($provider) {
case 'openstreetmap':
return [
'standard' => 'Standard',
];
case 'google':
return [
'roadmap' => 'Roadmap',
'satellite' => 'Satellite',
'hybrid' => 'Hybrid',
'terrain' => 'Terrain',
];
case 'mapbox':
return [
'streets-v11' => 'Streets',
'satellite-v9' => 'Satellite',
'outdoors-v11' => 'Outdoors',
'light-v10' => 'Light',
'dark-v10' => 'Dark',
];
case 'cartodb':
return [
'light' => 'Light',
'dark' => 'Dark',
'voyager' => 'Voyager',
];
case 'satellite':
return [
'satellite' => 'Satellite',
];
default:
return ['standard' => 'Standard'];
}
}
public function getMapConfig(string $provider = null): array
{
$provider = $provider ?: $this->getDefaultProvider();
$config = $this->getProviderConfig($provider);
if (!$config) {
throw new \InvalidArgumentException("Map provider '{$provider}' not found");
}
return [
'provider' => $provider,
'config' => $config,
'styles' => $this->getMapStyles($provider),
'enabled' => $this->isProviderEnabled($provider),
];
}
public function getAllProvidersStatus(): array
{
$providers = Config::get('services.maps.providers', []);
$status = [];
foreach ($providers as $key => $provider) {
$status[$key] = [
'name' => $provider['name'],
'free' => $provider['free'],
'requires_api_key' => $provider['requires_api_key'],
'enabled' => $this->isProviderEnabled($key),
'has_api_key' => !$provider['requires_api_key'] || !empty($provider['api_key']),
];
}
return $status;
}
}