Files
weather/src/Infrastructure/Provider/SlimWeatherProvider.php
Oh 120dee172b
Some checks failed
Tests / Tests PHP 7.4 (push) Has been cancelled
Tests / Tests PHP 8 (push) Has been cancelled
Tests / Tests PHP 8.1 (push) Has been cancelled
PKG
2025-09-04 14:47:18 +02:00

47 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Ohrionmartin\Weather\Infrastructure\Provider;
use Ohrionmartin\Weather\Application\Actions\Weather\GetWeatherAction;
use Ohrionmartin\Weather\Service\OpenWeatherClient;
use DI\ContainerBuilder;
use GuzzleHttp\Client;
use Psr\Container\ContainerInterface;
use Slim\App;
final class SlimWeatherProvider
{
public static function register(ContainerBuilder $cb): void
{
$cb->addDefinitions([
Client::class => function () {
return new Client([
'headers' => [
'Accept' => 'application/json',
'User-Agent' => 'Slim-Weather/1.0',
],
]);
},
OpenWeatherClient::class => function (ContainerInterface $c) {
$apiKey = $_ENV['OPENWEATHER_API_KEY'] ?? $_SERVER['OPENWEATHER_API_KEY'] ?? '';
$baseUrl = 'https://api.openweathermap.org/data/3.0';
if ($apiKey === '') {
throw new \RuntimeException('OPENWEATHER_API_KEY is not configured');
}
return new OpenWeatherClient($c->get(Client::class), $apiKey, $baseUrl);
},
]);
}
public static function routes(App $app): void
{
// Expose a ready-to-use endpoint for consumers (optional)
$app->get('/weather', GetWeatherAction::class);
}
}