47 lines
1.4 KiB
PHP
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);
|
|
}
|
|
}
|