Compare commits
4 Commits
acd9bfc2b3
...
70080bf80a
Author | SHA1 | Date | |
---|---|---|---|
70080bf80a | |||
6c23728c85 | |||
d7ff806129 | |||
13bdd096be |
4
.env
4
.env
@ -18,3 +18,7 @@
|
||||
APP_ENV=dev
|
||||
APP_SECRET=f27f6cb11fb594fcb5ab4591ec0aac77
|
||||
###< symfony/framework-bundle ###
|
||||
|
||||
###> sentry/sentry-symfony ###
|
||||
SENTRY_DSN=
|
||||
###< sentry/sentry-symfony ###
|
||||
|
@ -10,6 +10,7 @@
|
||||
"ext-iconv": "*",
|
||||
"grpc/grpc": "^1.57",
|
||||
"guzzlehttp/promises": "*",
|
||||
"nubium/this-should-never-happen-exception": "^1.0",
|
||||
"nyholm/psr7": "*",
|
||||
"open-telemetry/exporter-otlp": "^1.0",
|
||||
"open-telemetry/opentelemetry-auto-symfony": "^1.0@beta",
|
||||
@ -19,6 +20,7 @@
|
||||
"open-telemetry/sdk": "^1.0",
|
||||
"open-telemetry/transport-grpc": "^1.0",
|
||||
"php-http/httplug": "*",
|
||||
"sentry/sentry-symfony": "^4.13",
|
||||
"symfony/asset": "7.0.*",
|
||||
"symfony/asset-mapper": "7.0.*",
|
||||
"symfony/cache": "7.0.*",
|
||||
|
1175
composer.lock
generated
1175
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -6,4 +6,5 @@ return [
|
||||
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
|
||||
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
|
||||
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
|
||||
Sentry\SentryBundle\SentryBundle::class => ['prod' => true],
|
||||
];
|
||||
|
26
config/packages/sentry.yaml
Normal file
26
config/packages/sentry.yaml
Normal file
@ -0,0 +1,26 @@
|
||||
when@prod:
|
||||
sentry:
|
||||
dsn: '%env(SENTRY_DSN)%'
|
||||
# this hooks into critical paths of the framework (and vendors) to perform
|
||||
# automatic instrumentation (there might be some performance penalty)
|
||||
# https://docs.sentry.io/platforms/php/guides/symfony/performance/instrumentation/automatic-instrumentation/
|
||||
tracing:
|
||||
enabled: false
|
||||
|
||||
# If you are using Monolog, you also need this additional configuration to log the errors correctly:
|
||||
# https://docs.sentry.io/platforms/php/guides/symfony/#monolog-integration
|
||||
# register_error_listener: false
|
||||
# register_error_handler: false
|
||||
|
||||
# monolog:
|
||||
# handlers:
|
||||
# sentry:
|
||||
# type: sentry
|
||||
# level: !php/const Monolog\Logger::ERROR
|
||||
# hub_id: Sentry\State\HubInterface
|
||||
|
||||
# Uncomment these lines to register a log message processor that resolves PSR-3 placeholders
|
||||
# https://docs.sentry.io/platforms/php/guides/symfony/#monolog-integration
|
||||
# services:
|
||||
# Monolog\Processor\PsrLogMessageProcessor:
|
||||
# tags: { name: monolog.processor, handler: sentry }
|
@ -39,6 +39,13 @@ services:
|
||||
arguments:
|
||||
$username: '%app.usetreno.username%'
|
||||
$password: '%app.usetreno.password%'
|
||||
$retryCount: 2
|
||||
$retryWaitSeconds: 0.5
|
||||
|
||||
App\Service\Remote\UsetrenoQRCodeProvider:
|
||||
arguments:
|
||||
$retryCount: 2
|
||||
$retryWaitSeconds: 0.5
|
||||
|
||||
App\Service\CachedQRCodeGenerator:
|
||||
arguments:
|
||||
|
17
src/Controller/ExceptionController.php
Normal file
17
src/Controller/ExceptionController.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
* Used for testing error handling (sentry...)
|
||||
*/
|
||||
class ExceptionController extends AbstractController
|
||||
{
|
||||
#[Route("give-me-error-please/exception")]
|
||||
public function makeException(): void {
|
||||
throw new \InvalidArgumentException("There is exception");
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity\Remote\Usetreno;
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Remote\Exception;
|
||||
|
||||
class UsetrenoQRCodeRemoteServerErrorException extends UsetrenoQRCodeException
|
||||
{
|
||||
|
||||
}
|
59
src/Service/Remote/RetryingFailClientTrait.php
Normal file
59
src/Service/Remote/RetryingFailClientTrait.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Remote;
|
||||
|
||||
use App\Service\Remote\Exception\AuthorizeException;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||
|
||||
trait RetryingFailClientTrait
|
||||
{
|
||||
/**
|
||||
* Lepsi zopakovat request kvuli drobnemu vypadku site nebo sluzby (u nedestruktivni operace)
|
||||
* nez hodit klientovi rovnou 500
|
||||
*
|
||||
* @param int $count
|
||||
* @param float $sleep
|
||||
* @param array<string> $catchableExceptions
|
||||
* @param LoggerInterface $logger
|
||||
* @param callable $callback
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function retryingFailRequest(
|
||||
int $count,
|
||||
float $sleep,
|
||||
array $catchableExceptions,
|
||||
LoggerInterface $logger,
|
||||
callable $callback
|
||||
): mixed {
|
||||
for ($i = 0; ; $i++) {
|
||||
try {
|
||||
return $callback();
|
||||
} catch (Exception $e) {
|
||||
foreach ($catchableExceptions as $exceptionClass) {
|
||||
if ($e instanceof $exceptionClass) {
|
||||
$logger->error("transport: fail request retrying... got catchable exception", [
|
||||
'exception' => $e,
|
||||
'try' => $i
|
||||
]);
|
||||
|
||||
usleep((int) ($sleep * 1_000_000));
|
||||
|
||||
if ($i == $count) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
// phpstan fail
|
||||
return null;
|
||||
}
|
||||
}
|
@ -5,7 +5,9 @@ namespace App\Service\Remote;
|
||||
|
||||
use App\Entity\Remote\Usetreno\AuthRequest;
|
||||
use App\Service\Remote\Exception\AuthorizeException;
|
||||
use Nubium\Exception\ThisShouldNeverHappenException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
@ -16,12 +18,16 @@ use Symfony\Contracts\HttpClient\ResponseStreamInterface;
|
||||
|
||||
class UsetrenoHttpClient implements HttpClientInterface
|
||||
{
|
||||
use RetryingFailClientTrait;
|
||||
|
||||
const AUTHORIZE_API = "https://topapi.top-test.cz/chameleon/api/v1/token";
|
||||
|
||||
protected ?string $authorizationToken = null;
|
||||
|
||||
public function __construct(protected HttpClientInterface $innerClient, protected readonly LoggerInterface $logger, protected readonly string $username,
|
||||
protected readonly string $password) { }
|
||||
protected readonly string $password,
|
||||
protected readonly float $retryWaitSeconds,
|
||||
protected readonly int $retryCount) { }
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
@ -88,6 +94,8 @@ class UsetrenoHttpClient implements HttpClientInterface
|
||||
"AUTHORIZE_API" => static::AUTHORIZE_API
|
||||
]);
|
||||
|
||||
$responseData = $this->retryingFailRequest($this->retryCount, $this->retryWaitSeconds,
|
||||
[AuthorizeException::class, TransportException::class], $this->logger, function() {
|
||||
$rq = $this->innerClient->request(
|
||||
"POST",
|
||||
static::AUTHORIZE_API,
|
||||
@ -110,6 +118,13 @@ class UsetrenoHttpClient implements HttpClientInterface
|
||||
throw new AuthorizeException("Return code is not 200 OK (got: code: $statusCode)");
|
||||
}
|
||||
|
||||
return $responseData;
|
||||
});
|
||||
|
||||
if (!is_string($responseData)) {
|
||||
throw new ThisShouldNeverHappenException("responseData is not a string");
|
||||
}
|
||||
|
||||
$this->authorizationToken = $this->processAuthorizeResponse($responseData);
|
||||
$this->innerClient = $this->innerClient->withOptions([
|
||||
'headers' => [
|
||||
|
@ -7,16 +7,23 @@ use App\Entity\DTO\QRCode\QRCode;
|
||||
use App\Service\CacheableQRCodeGeneratorInterface;
|
||||
use App\Service\Remote\Edge\QRCodeEntityConverter;
|
||||
use App\Service\Remote\Exception\UsetrenoQRCodeException;
|
||||
use App\Service\Remote\Exception\UsetrenoQRCodeRemoteServerErrorException;
|
||||
use Nubium\Exception\ThisShouldNeverHappenException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||
|
||||
class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
{
|
||||
use RetryingFailClientTrait;
|
||||
|
||||
const QRCODE_API = 'https://topapi.top-test.cz/chameleon/api/v1/qr-code/create-for-bank-account-payment';
|
||||
const QRCODE_METHOD = 'POST';
|
||||
|
||||
public function __construct(protected readonly LoggerInterface $logger,
|
||||
protected readonly UsetrenoHttpClient $client,
|
||||
protected readonly QRCodeEntityConverter $codeEntityConverter) { }
|
||||
protected readonly QRCodeEntityConverter $codeEntityConverter,
|
||||
protected readonly float $retryWaitSeconds,
|
||||
protected readonly int $retryCount) { }
|
||||
|
||||
public function generateQRCodeFromEntity(QRCode $entity): string
|
||||
{
|
||||
@ -27,6 +34,9 @@ class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
"edgeEntity" => $edgeEntity
|
||||
]);
|
||||
|
||||
$responseData = $this->retryingFailRequest($this->retryCount, $this->retryWaitSeconds,
|
||||
[TransportException::class, UsetrenoQRCodeRemoteServerErrorException::class], $this->logger,
|
||||
function() use ($edgeEntity) {
|
||||
$response = $this->client->request(static::QRCODE_METHOD, static::QRCODE_API, [
|
||||
'json' => $edgeEntity,
|
||||
]);
|
||||
@ -41,6 +51,10 @@ class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
"content" => $responseData,
|
||||
]);
|
||||
|
||||
if ($statusCode > 500) {
|
||||
throw new UsetrenoQRCodeRemoteServerErrorException("Return code is not 200 OK (got: code: $statusCode)");
|
||||
}
|
||||
|
||||
throw new UsetrenoQRCodeException("Return code is not 200 OK (got: code: $statusCode)");
|
||||
}
|
||||
|
||||
@ -48,6 +62,13 @@ class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
"responseContent" => $responseData,
|
||||
]);
|
||||
|
||||
return $responseData;
|
||||
});
|
||||
|
||||
if (!is_string($responseData)) {
|
||||
throw new ThisShouldNeverHappenException("responseData is not a string");
|
||||
}
|
||||
|
||||
return $this->parseBase64String($this->processQRCodeResponseEntity($responseData));
|
||||
}
|
||||
|
||||
|
12
symfony.lock
12
symfony.lock
@ -49,6 +49,18 @@
|
||||
"tests/bootstrap.php"
|
||||
]
|
||||
},
|
||||
"sentry/sentry-symfony": {
|
||||
"version": "4.13",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes-contrib",
|
||||
"branch": "main",
|
||||
"version": "4.6",
|
||||
"ref": "153de5f041f7e8a9c19f3674b800b76be0e6fd90"
|
||||
},
|
||||
"files": [
|
||||
"config/packages/sentry.yaml"
|
||||
]
|
||||
},
|
||||
"symfony/asset-mapper": {
|
||||
"version": "7.0",
|
||||
"recipe": {
|
||||
|
56
tests/Service/Remote/RetryingFailClientTraitTest.php
Normal file
56
tests/Service/Remote/RetryingFailClientTraitTest.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Service\Remote;
|
||||
|
||||
use App\Service\Remote\RetryingFailClientTrait;
|
||||
use App\Tests\Common\LoggerTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class RetryingFailClientTraitTest extends TestCase {
|
||||
use LoggerTrait;
|
||||
|
||||
private int $callCount = 0;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp(); // TODO: Change the autogenerated stub
|
||||
$this->callCount = 0;
|
||||
}
|
||||
|
||||
public function testSuccess() {
|
||||
$trait = new class {
|
||||
use RetryingFailClientTrait {
|
||||
retryingFailRequest as public; // make the method public
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
$result = $trait->retryingFailRequest(2, 0, [\RuntimeException::class], $this->getLogger(), function () {
|
||||
$this->callCount = $this->callCount + 1;
|
||||
return 'foo';
|
||||
});
|
||||
|
||||
$this->assertEquals(1, $this->callCount);
|
||||
$this->assertEquals('foo', $result);
|
||||
}
|
||||
public function testRetyingFail() {
|
||||
$trait = new class {
|
||||
use RetryingFailClientTrait {
|
||||
retryingFailRequest as public; // make the method public
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
$trait->retryingFailRequest(2, 0, [\RuntimeException::class], $this->getLogger(), function () {
|
||||
$this->callCount = $this->callCount + 1;
|
||||
throw new \RuntimeException("test");
|
||||
});
|
||||
} catch (\RuntimeException) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
$this->assertEquals(3, $this->callCount);
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ class UsetrenoHttpClientTest extends TestCase
|
||||
]);
|
||||
$authorizedRequestResponse = clone $authMockResponse;
|
||||
$mockedClient = new MockHttpClient([$authMockResponse, $authorizedRequestResponse]);
|
||||
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar");
|
||||
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar", 0, 0);
|
||||
$client->request("POST", "https://www.root.cz/");
|
||||
$this->assertEquals("https://topapi.top-test.cz/chameleon/api/v1/token", $authMockResponse->getRequestUrl());
|
||||
$headers = $authorizedRequestResponse->getRequestOptions()['headers'];
|
||||
@ -48,7 +48,7 @@ class UsetrenoHttpClientTest extends TestCase
|
||||
|
||||
$authorizedRequestResponse = clone $authMockResponse;
|
||||
$mockedClient = new MockHttpClient([$authMockResponse, $authorizedRequestResponse]);
|
||||
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar");
|
||||
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar", 0, 0);
|
||||
$client->request("POST", "https://www.root.cz/");
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ class UsetrenoQRCodeProviderTest extends TestCase
|
||||
->method('convert')
|
||||
->will($this->returnValue($edgeEntity));
|
||||
|
||||
return new UsetrenoQRCodeProvider($this->getLogger(), $mock, $converterMock);
|
||||
return new UsetrenoQRCodeProvider($this->getLogger(), $mock, $converterMock, 0, 0);
|
||||
}
|
||||
|
||||
protected function createQRCodeEntityPair() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user