initital commit

This commit is contained in:
2025-01-26 21:17:23 +01:00
commit 2a7345ba56
72 changed files with 9458 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace App\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
class RetryableHttpClient implements HttpClientInterface
{
public function __construct(
protected readonly LoggerInterface $logger,
protected readonly HttpClientInterface $client,
protected int $maxRetries = 3,
) {
}
/**
* @param array<mixed> $options
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$lastException = new \InvalidArgumentException("$this->maxRetries < 1");
for ($i = 1; $i <= $this->maxRetries; ++$i) {
try {
$response = $this->client->request($method, $url, $options);
// TODO: dynamic filters for codes & handle 400 error, slow down response ...
if ($response->getStatusCode() !== 200) {
$this->logger->warning("Response $method on $url failed.", [
'statusCode' => $response->getStatusCode(),
]);
// TODO: better exception
$lastException = new \RuntimeException("Invalid status code, accept: 200, got " . $response->getStatusCode());
continue;
}
return $response;
} catch (\Exception $exception) {
$this->logger->error("Response $method on $url failed.", [
'exception' => $exception,
]);
$lastException = $exception;
}
}
throw $lastException;
}
public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface
{
$lastException = new \InvalidArgumentException("$this->maxRetries < 1");
for ($i = 1; $i <= $this->maxRetries; ++$i) {
try {
return $this->client->stream($responses, $timeout);
} catch (\Exception $exception) {
$this->logger->error("Stream failed.", [
'exception' => $exception,
]);
$lastException = $exception;
}
}
throw $lastException;
}
/**
* @param array<mixed> $options
*/
public function withOptions(array $options): static
{
return new static($this->logger, $this->client->withOptions($options), $this->maxRetries); // @phpstan-ignore new.static
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Utils\Weather;
enum WeatherInterval
{
case DAILY;
}