captcha-queries.php: refactor remote captcha queries to use DI and abstract the implementation better

This commit is contained in:
Zankaria
2024-08-15 15:11:32 +02:00
parent e825e7aac5
commit 933594194c
3 changed files with 105 additions and 62 deletions

View File

@@ -3,6 +3,9 @@ namespace Vichan;
use RuntimeException;
use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers};
use Vichan\Service\HCaptchaQuery;
use Vichan\Service\ReCaptchaQuery;
use Vichan\Service\RemoteCaptchaQuery;
defined('TINYBOARD') or exit;
@@ -53,6 +56,17 @@ function build_context(array $config): Context {
HttpDriver::class => function($c) {
$config = $c->get('config');
return HttpDrivers::getHttpDriver($config['upload_by_url_timeout'], $config['max_filesize']);
},
RemoteCaptchaQuery::class => function($c) {
$config = $c->get('config');
$http = $c->get(HttpDriver::class);
if ($config['recaptcha']) {
return new ReCaptchaQuery($http, $config['recaptcha_private']);
} elseif ($config['hcaptcha']) {
return new HCaptchaQuery($http, $config['hcaptcha_private'], $config['hcaptcha_public']);
} else {
throw new RuntimeException('No remote captcha service available');
}
}
]);
}