dsfd
This commit is contained in:
@@ -3,18 +3,6 @@ declare(strict_types=1);
|
||||
|
||||
use App\ModuleConfigException;
|
||||
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
$prefix = 'Modules\\MiningChecker\\';
|
||||
if (!str_starts_with($class, $prefix)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file = dirname(__DIR__) . '/mining-checker/src/' . str_replace('\\', '/', substr($class, strlen($prefix))) . '.php';
|
||||
if (is_file($file)) {
|
||||
require_once $file;
|
||||
}
|
||||
});
|
||||
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
$prefix = 'Modules\\Boersenchecker\\';
|
||||
if (!str_starts_with($class, $prefix)) {
|
||||
@@ -228,34 +216,11 @@ $mm->registerFunction($moduleName, 'fx_service', static function (): ?object {
|
||||
try {
|
||||
return module_fn('fx-rates', 'service');
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_dir(dirname(__DIR__) . '/mining-checker')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$config = \Modules\MiningChecker\Infrastructure\ModuleConfig::load(dirname(__DIR__) . '/mining-checker');
|
||||
$repo = new \Modules\MiningChecker\Infrastructure\MiningRepository(
|
||||
\Modules\MiningChecker\Infrastructure\ConnectionFactory::make($config),
|
||||
$config->tablePrefix()
|
||||
);
|
||||
$fx = $config->fx();
|
||||
|
||||
return new \Modules\MiningChecker\Domain\FxService(
|
||||
$repo,
|
||||
(string) ($fx['url'] ?? 'https://currencyapi.net'),
|
||||
(string) ($fx['currencies_url'] ?? ($fx['url'] ?? 'https://currencyapi.net')),
|
||||
(int) ($fx['timeout'] ?? 10),
|
||||
(int) ($fx['cache_ttl'] ?? 21600),
|
||||
false,
|
||||
(string) ($fx['provider'] ?? 'currencyapi'),
|
||||
(string) ($fx['api_key'] ?? '')
|
||||
);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'fx_refresh', static function (string $baseCurrency = 'EUR', float $maxAgeHours = 6.0): array {
|
||||
@@ -263,7 +228,7 @@ $mm->registerFunction($moduleName, 'fx_refresh', static function (string $baseCu
|
||||
if (!$service || !method_exists($service, 'ensureFreshLatestRates')) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'FX-Service ist aktuell nicht verfuegbar.',
|
||||
'message' => 'Das Modul fx-rates ist aktuell nicht verfuegbar oder nicht aktiviert.',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -284,6 +249,119 @@ $mm->registerFunction($moduleName, 'fx_refresh', static function (string $baseCu
|
||||
}
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'fx_prepare_fetch', static function (
|
||||
string $baseCurrency = 'EUR',
|
||||
array $currencies = [],
|
||||
float $maxAgeHours = 6.0
|
||||
): array {
|
||||
$service = module_fn('boersenchecker', 'fx_service');
|
||||
if (!$service || !method_exists($service, 'ensureFreshLatestRates')) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Das Modul fx-rates ist aktuell nicht verfuegbar oder nicht aktiviert.',
|
||||
];
|
||||
}
|
||||
|
||||
$baseCurrency = strtoupper(trim($baseCurrency)) ?: 'EUR';
|
||||
$currencies = array_values(array_unique(array_filter(array_map(
|
||||
static fn (mixed $code): string => strtoupper(trim((string) $code)),
|
||||
$currencies
|
||||
), static fn (string $code): bool => $code !== '')));
|
||||
|
||||
try {
|
||||
$result = $service->ensureFreshLatestRates($maxAgeHours, $baseCurrency, $currencies);
|
||||
return [
|
||||
'ok' => true,
|
||||
'message' => !empty($result['reused']) ? 'Vorhandene FX-Daten weiterverwendet.' : 'FX-Daten aktualisiert.',
|
||||
'result' => $result,
|
||||
'fetch_id' => is_numeric($result['fetch_id'] ?? null) ? (int) $result['fetch_id'] : null,
|
||||
'reused' => !empty($result['reused']),
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'FX-Aktualisierung fehlgeschlagen: ' . $e->getMessage(),
|
||||
];
|
||||
}
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'fx_source_with_fetch_id', static function (string $source, ?int $fetchId = null): string {
|
||||
$source = trim($source) !== '' ? trim($source) : 'manual';
|
||||
if ($fetchId === null || $fetchId <= 0) {
|
||||
return preg_replace('/\|fx_fetch:\d+$/', '', $source) ?: $source;
|
||||
}
|
||||
|
||||
$source = preg_replace('/\|fx_fetch:\d+$/', '', $source) ?: $source;
|
||||
return $source . '|fx_fetch:' . $fetchId;
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'fx_extract_fetch_id', static function (?string $source): ?int {
|
||||
$source = trim((string) $source);
|
||||
if ($source === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (preg_match('/\|fx_fetch:(\d+)$/', $source, $matches) === 1) {
|
||||
$fetchId = (int) ($matches[1] ?? 0);
|
||||
return $fetchId > 0 ? $fetchId : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'fx_convert_with_fetch', static function (
|
||||
?float $amount,
|
||||
?string $fromCurrency,
|
||||
?string $toCurrency,
|
||||
?int $fetchId = null
|
||||
): ?float {
|
||||
if ($amount === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$from = strtoupper(trim((string) $fromCurrency));
|
||||
$to = strtoupper(trim((string) $toCurrency));
|
||||
if ($from === '' || $to === '') {
|
||||
return null;
|
||||
}
|
||||
if ($from === $to) {
|
||||
return $amount;
|
||||
}
|
||||
|
||||
$service = module_fn('boersenchecker', 'fx_service');
|
||||
if (!$service) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$normalizedFetchId = $fetchId !== null && $fetchId > 0 ? $fetchId : null;
|
||||
if ($normalizedFetchId !== null && method_exists($service, 'snapshotByFetchId')) {
|
||||
try {
|
||||
$snapshot = $service->snapshotByFetchId($normalizedFetchId, null, [$from, $to]);
|
||||
if (is_array($snapshot)) {
|
||||
$base = strtoupper(trim((string) ($snapshot['base_currency'] ?? '')));
|
||||
$rates = is_array($snapshot['rates'] ?? null) ? $snapshot['rates'] : [];
|
||||
$fromRate = $from === $base ? 1.0 : (is_numeric($rates[$from] ?? null) ? (float) $rates[$from] : null);
|
||||
$toRate = $to === $base ? 1.0 : (is_numeric($rates[$to] ?? null) ? (float) $rates[$to] : null);
|
||||
if ($fromRate !== null && $fromRate > 0 && $toRate !== null && $toRate > 0) {
|
||||
return $amount * ($toRate / $fromRate);
|
||||
}
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!method_exists($service, 'convert')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$value = $service->convert($amount, $from, $to);
|
||||
return is_numeric($value) ? (float) $value : null;
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'alpha_vantage_request', static function (
|
||||
string $functionName,
|
||||
array $params = []
|
||||
@@ -648,6 +726,16 @@ $mm->registerFunction($moduleName, 'scheduled_refresh_quotes', static function (
|
||||
];
|
||||
}
|
||||
|
||||
$quoteCurrencies = array_values(array_unique(array_filter(array_map(
|
||||
static fn (array $row): string => strtoupper(trim((string) ($row['quote_currency'] ?? ''))),
|
||||
$candidates
|
||||
), static fn (string $code): bool => $code !== '')));
|
||||
$fxResult = module_fn('boersenchecker', 'fx_prepare_fetch', $defaultReportCurrency, $quoteCurrencies, (float) (($settings['fx_max_age_hours'] ?? null) ?: 6));
|
||||
if (empty($fxResult['ok'])) {
|
||||
return $fxResult;
|
||||
}
|
||||
$fxFetchId = is_numeric($fxResult['fetch_id'] ?? null) ? (int) $fxResult['fetch_id'] : null;
|
||||
|
||||
$bulkResult = module_fn('boersenchecker', 'alpha_vantage_fetch_quotes', $candidates);
|
||||
if (empty($bulkResult['ok'])) {
|
||||
return [
|
||||
@@ -673,7 +761,7 @@ $mm->registerFunction($moduleName, 'scheduled_refresh_quotes', static function (
|
||||
(float) $quote['price'],
|
||||
strtoupper(trim((string) ($quote['currency'] ?? $row['quote_currency'] ?? $defaultReportCurrency))) ?: $defaultReportCurrency,
|
||||
(string) ($quote['fetched_at'] ?? gmdate('Y-m-d H:i:s')),
|
||||
(string) ($quote['source'] ?? 'alphavantage:global_quote')
|
||||
(string) module_fn('boersenchecker', 'fx_source_with_fetch_id', (string) ($quote['source'] ?? 'alphavantage:global_quote'), $fxFetchId)
|
||||
);
|
||||
if (!empty($storeResult['inserted'])) {
|
||||
$updated++;
|
||||
|
||||
Reference in New Issue
Block a user