cyxc
This commit is contained in:
@@ -111,231 +111,6 @@ final class MiningRepository
|
||||
]);
|
||||
}
|
||||
|
||||
public function listCurrencies(): array
|
||||
{
|
||||
$this->debug?->add('db.listCurrencies.start');
|
||||
$stmt = $this->pdo->query(
|
||||
'SELECT * FROM ' . $this->table('currencies') . ' WHERE ' . ($this->driver === 'pgsql' ? 'is_active = TRUE' : 'is_active = 1') . ' ORDER BY sort_order ASC, code ASC'
|
||||
);
|
||||
$rows = $this->normalizeRows($stmt->fetchAll() ?: []);
|
||||
$this->debug?->add('db.listCurrencies.end', ['rows' => count($rows)]);
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function listCurrencyAliases(): array
|
||||
{
|
||||
$stmt = $this->pdo->query(
|
||||
'SELECT
|
||||
a.alias_code,
|
||||
a.currency_code,
|
||||
c.name AS currency_name,
|
||||
a.created_at
|
||||
FROM ' . $this->table('currency_aliases') . ' a
|
||||
INNER JOIN ' . $this->table('currencies') . ' c ON c.code = a.currency_code
|
||||
ORDER BY a.alias_code ASC'
|
||||
);
|
||||
return $this->normalizeRows($stmt->fetchAll() ?: []);
|
||||
}
|
||||
|
||||
public function resolveCurrencyCode(string $code): ?array
|
||||
{
|
||||
$normalizedCode = strtoupper(trim($code));
|
||||
if ($normalizedCode === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$currencyStmt = $this->pdo->prepare('SELECT * FROM ' . $this->table('currencies') . ' WHERE code = :code LIMIT 1');
|
||||
$currencyStmt->execute(['code' => $normalizedCode]);
|
||||
$currency = $currencyStmt->fetch();
|
||||
if (is_array($currency)) {
|
||||
return [
|
||||
'input_code' => $normalizedCode,
|
||||
'code' => $normalizedCode,
|
||||
'matched_via' => 'code',
|
||||
'currency' => $this->normalizeRow($currency),
|
||||
];
|
||||
}
|
||||
|
||||
if (!$this->tableExists('currency_aliases')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$aliasStmt = $this->pdo->prepare(
|
||||
'SELECT
|
||||
a.alias_code,
|
||||
a.currency_code,
|
||||
c.name AS currency_name,
|
||||
c.symbol AS currency_symbol,
|
||||
c.is_active,
|
||||
c.sort_order
|
||||
FROM ' . $this->table('currency_aliases') . ' a
|
||||
INNER JOIN ' . $this->table('currencies') . ' c ON c.code = a.currency_code
|
||||
WHERE a.alias_code = :alias_code
|
||||
LIMIT 1'
|
||||
);
|
||||
$aliasStmt->execute(['alias_code' => $normalizedCode]);
|
||||
$alias = $aliasStmt->fetch();
|
||||
if (!is_array($alias)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'input_code' => $normalizedCode,
|
||||
'code' => (string) $alias['currency_code'],
|
||||
'matched_via' => 'alias',
|
||||
'alias_code' => (string) $alias['alias_code'],
|
||||
'currency' => $this->normalizeRow([
|
||||
'code' => $alias['currency_code'],
|
||||
'name' => $alias['currency_name'],
|
||||
'symbol' => $alias['currency_symbol'] ?? null,
|
||||
'is_active' => $alias['is_active'] ?? 1,
|
||||
'sort_order' => $alias['sort_order'] ?? 1000,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function saveCurrencyAlias(string $aliasCode, string $currencyCode): array
|
||||
{
|
||||
$normalizedAlias = strtoupper(trim($aliasCode));
|
||||
$normalizedCurrency = strtoupper(trim($currencyCode));
|
||||
|
||||
$stmt = $this->pdo->prepare($this->driver === 'pgsql'
|
||||
? 'INSERT INTO ' . $this->table('currency_aliases') . ' (alias_code, currency_code)
|
||||
VALUES (:alias_code, :currency_code)
|
||||
ON CONFLICT (alias_code) DO UPDATE SET currency_code = EXCLUDED.currency_code
|
||||
RETURNING *'
|
||||
: 'INSERT INTO ' . $this->table('currency_aliases') . ' (alias_code, currency_code)
|
||||
VALUES (:alias_code, :currency_code)
|
||||
ON DUPLICATE KEY UPDATE currency_code = VALUES(currency_code)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'alias_code' => $normalizedAlias,
|
||||
'currency_code' => $normalizedCurrency,
|
||||
]);
|
||||
|
||||
if ($this->driver === 'pgsql') {
|
||||
$row = $stmt->fetch();
|
||||
return is_array($row) ? $this->normalizeRow($row) : ['alias_code' => $normalizedAlias, 'currency_code' => $normalizedCurrency];
|
||||
}
|
||||
|
||||
return ['alias_code' => $normalizedAlias, 'currency_code' => $normalizedCurrency];
|
||||
}
|
||||
|
||||
public function saveCurrency(array $currency): void
|
||||
{
|
||||
$this->debug?->add('db.saveCurrency.start', ['code' => $currency['code'] ?? null]);
|
||||
$stmt = $this->pdo->prepare($this->driver === 'pgsql'
|
||||
? 'INSERT INTO ' . $this->table('currencies') . ' (code, name, symbol, is_active, is_crypto, sort_order)
|
||||
VALUES (:code, :name, :symbol, :is_active, :is_crypto, :sort_order)
|
||||
ON CONFLICT (code) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
symbol = EXCLUDED.symbol,
|
||||
is_active = EXCLUDED.is_active,
|
||||
is_crypto = EXCLUDED.is_crypto,
|
||||
sort_order = EXCLUDED.sort_order'
|
||||
: 'INSERT INTO ' . $this->table('currencies') . ' (code, name, symbol, is_active, is_crypto, sort_order)
|
||||
VALUES (:code, :name, :symbol, :is_active, :is_crypto, :sort_order)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
name = VALUES(name),
|
||||
symbol = VALUES(symbol),
|
||||
is_active = VALUES(is_active),
|
||||
is_crypto = VALUES(is_crypto),
|
||||
sort_order = VALUES(sort_order)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'code' => $currency['code'],
|
||||
'name' => $currency['name'],
|
||||
'symbol' => $currency['symbol'],
|
||||
'is_active' => $currency['is_active'],
|
||||
'is_crypto' => $currency['is_crypto'] ?? 0,
|
||||
'sort_order' => $currency['sort_order'],
|
||||
]);
|
||||
$this->debug?->add('db.saveCurrency.end', ['code' => $currency['code'] ?? null]);
|
||||
}
|
||||
|
||||
public function saveCurrencies(array $currencies): int
|
||||
{
|
||||
if ($currencies === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$this->debug?->add('db.saveCurrencies.start', ['count' => count($currencies)]);
|
||||
|
||||
$statement = $this->pdo->prepare($this->driver === 'pgsql'
|
||||
? 'INSERT INTO ' . $this->table('currencies') . ' (code, name, symbol, is_active, is_crypto, sort_order)
|
||||
VALUES (:code, :name, :symbol, :is_active, :is_crypto, :sort_order)
|
||||
ON CONFLICT (code) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
symbol = EXCLUDED.symbol,
|
||||
is_active = EXCLUDED.is_active,
|
||||
is_crypto = EXCLUDED.is_crypto,
|
||||
sort_order = EXCLUDED.sort_order'
|
||||
: 'INSERT INTO ' . $this->table('currencies') . ' (code, name, symbol, is_active, is_crypto, sort_order)
|
||||
VALUES (:code, :name, :symbol, :is_active, :is_crypto, :sort_order)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
name = VALUES(name),
|
||||
symbol = VALUES(symbol),
|
||||
is_active = VALUES(is_active),
|
||||
is_crypto = VALUES(is_crypto),
|
||||
sort_order = VALUES(sort_order)'
|
||||
);
|
||||
|
||||
$count = 0;
|
||||
$startedTransaction = false;
|
||||
if (!$this->pdo->inTransaction()) {
|
||||
$this->pdo->beginTransaction();
|
||||
$startedTransaction = true;
|
||||
}
|
||||
|
||||
try {
|
||||
foreach ($currencies as $currency) {
|
||||
if (!is_array($currency) || empty($currency['code'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$statement->execute([
|
||||
'code' => $currency['code'],
|
||||
'name' => $currency['name'] ?? $currency['code'],
|
||||
'symbol' => $currency['symbol'] ?? $currency['code'],
|
||||
'is_active' => $currency['is_active'] ?? 1,
|
||||
'is_crypto' => $currency['is_crypto'] ?? 0,
|
||||
'sort_order' => $currency['sort_order'] ?? 1000,
|
||||
]);
|
||||
$count++;
|
||||
}
|
||||
|
||||
if ($startedTransaction) {
|
||||
$this->pdo->commit();
|
||||
}
|
||||
$this->debug?->add('db.saveCurrencies.end', ['count' => $count]);
|
||||
} catch (\Throwable $exception) {
|
||||
if ($startedTransaction && $this->pdo->inTransaction()) {
|
||||
$this->pdo->rollBack();
|
||||
}
|
||||
$this->debug?->add('db.saveCurrencies.error', ['message' => $exception->getMessage()]);
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
public function ensureCurrencyCode(string $code, ?string $name = null): void
|
||||
{
|
||||
$normalizedCode = strtoupper(trim($code));
|
||||
if ($normalizedCode === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->saveCurrency([
|
||||
'code' => substr($normalizedCode, 0, 10),
|
||||
'name' => $name !== null && trim($name) !== '' ? trim($name) : $normalizedCode,
|
||||
'symbol' => substr($normalizedCode, 0, 8),
|
||||
'is_active' => 1,
|
||||
'is_crypto' => $this->isCryptoCode($normalizedCode) ? 1 : 0,
|
||||
'sort_order' => 1000,
|
||||
]);
|
||||
}
|
||||
|
||||
public function tableExists(string $logicalName): bool
|
||||
{
|
||||
$tableName = $this->table($logicalName);
|
||||
@@ -1099,32 +874,6 @@ final class MiningRepository
|
||||
return ['fetch' => null, 'rates' => []];
|
||||
}
|
||||
|
||||
$currenciesToEnsure = [
|
||||
[
|
||||
'code' => substr($baseCurrency, 0, 10),
|
||||
'name' => $baseCurrency,
|
||||
'symbol' => substr($baseCurrency, 0, 8),
|
||||
'is_active' => 1,
|
||||
'is_crypto' => $this->isCryptoCode($baseCurrency) ? 1 : 0,
|
||||
'sort_order' => 1000,
|
||||
],
|
||||
];
|
||||
foreach (array_keys($rates) as $currencyCode) {
|
||||
$normalizedCurrencyCode = strtoupper(trim((string) $currencyCode));
|
||||
if ($normalizedCurrencyCode === '') {
|
||||
continue;
|
||||
}
|
||||
$currenciesToEnsure[] = [
|
||||
'code' => substr($normalizedCurrencyCode, 0, 10),
|
||||
'name' => $normalizedCurrencyCode,
|
||||
'symbol' => substr($normalizedCurrencyCode, 0, 8),
|
||||
'is_active' => 1,
|
||||
'is_crypto' => $this->isCryptoCode($normalizedCurrencyCode) ? 1 : 0,
|
||||
'sort_order' => 1000,
|
||||
];
|
||||
}
|
||||
$this->saveCurrencies($currenciesToEnsure);
|
||||
|
||||
if ($this->driver === 'pgsql') {
|
||||
$fetchStmt = $this->pdo->prepare(
|
||||
'INSERT INTO ' . $this->table('fx_fetches') . ' (
|
||||
@@ -1247,16 +996,6 @@ final class MiningRepository
|
||||
$provider = trim($provider) !== '' ? trim($provider) : 'currencyapi';
|
||||
$fetchedAt = $this->currentUtcTimestamp();
|
||||
$normalizedRates = [];
|
||||
$currenciesToEnsure = [
|
||||
[
|
||||
'code' => substr($baseCurrency, 0, 10),
|
||||
'name' => $baseCurrency,
|
||||
'symbol' => substr($baseCurrency, 0, 8),
|
||||
'is_active' => 1,
|
||||
'is_crypto' => $this->isCryptoCode($baseCurrency) ? 1 : 0,
|
||||
'sort_order' => 1000,
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($rates as $currencyCode => $rate) {
|
||||
if (!is_numeric($rate)) {
|
||||
@@ -1269,14 +1008,6 @@ final class MiningRepository
|
||||
}
|
||||
|
||||
$normalizedRates[$normalizedCurrencyCode] = (float) $rate;
|
||||
$currenciesToEnsure[] = [
|
||||
'code' => substr($normalizedCurrencyCode, 0, 10),
|
||||
'name' => $normalizedCurrencyCode,
|
||||
'symbol' => substr($normalizedCurrencyCode, 0, 8),
|
||||
'is_active' => 1,
|
||||
'is_crypto' => $this->isCryptoCode($normalizedCurrencyCode) ? 1 : 0,
|
||||
'sort_order' => 1000,
|
||||
];
|
||||
}
|
||||
|
||||
$this->debug?->add('db.saveFxFetch.start', [
|
||||
@@ -1304,14 +1035,6 @@ final class MiningRepository
|
||||
}
|
||||
|
||||
try {
|
||||
$this->debug?->add('db.saveFxFetch.saveCurrencies.start', [
|
||||
'currency_count' => count($currenciesToEnsure),
|
||||
]);
|
||||
$this->saveCurrencies($currenciesToEnsure);
|
||||
$this->debug?->add('db.saveFxFetch.saveCurrencies.end', [
|
||||
'currency_count' => count($currenciesToEnsure),
|
||||
]);
|
||||
|
||||
if ($this->driver === 'pgsql') {
|
||||
$this->debug?->add('db.saveFxFetch.insertFetch.start', [
|
||||
'driver' => $this->driver,
|
||||
@@ -1459,21 +1182,10 @@ final class MiningRepository
|
||||
}
|
||||
}
|
||||
|
||||
public function listActiveFiatCurrencies(): array
|
||||
{
|
||||
$currencies = $this->listCurrencies();
|
||||
|
||||
return array_values(array_filter($currencies, static function (array $currency): bool {
|
||||
$code = strtoupper((string) ($currency['code'] ?? ''));
|
||||
return $code !== '' && empty($currency['is_crypto']);
|
||||
}));
|
||||
}
|
||||
|
||||
private function table(string $logicalName): string
|
||||
{
|
||||
return match ($logicalName) {
|
||||
'projects' => $this->prefix . 'projects',
|
||||
'currencies' => $this->prefix . 'currencies',
|
||||
'settings' => $this->prefix . 'settings',
|
||||
'cost_plans' => $this->prefix . 'cost_plans',
|
||||
'measurements' => $this->prefix . 'measurements',
|
||||
|
||||
@@ -213,18 +213,6 @@ final class SchemaManager
|
||||
$this->upgradeTargetOfferColumn();
|
||||
$applied[] = 'target_offer_column';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'currency_aliases')) {
|
||||
$this->ensureCurrencyAliasesTable();
|
||||
$applied[] = 'currency_aliases_table';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'currencies') && !$this->columnExists($this->prefix . 'currencies', 'is_crypto')) {
|
||||
$this->upgradeCurrenciesClassificationColumns();
|
||||
$applied[] = 'currency_classification';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'currencies')) {
|
||||
$this->ensureCurrencyForeignKeys();
|
||||
$applied[] = 'currency_foreign_keys';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'miner_offers') && (
|
||||
!$this->columnExists($this->prefix . 'miner_offers', 'base_price_amount') ||
|
||||
!$this->columnExists($this->prefix . 'miner_offers', 'base_price_currency') ||
|
||||
@@ -264,7 +252,6 @@ final class SchemaManager
|
||||
{
|
||||
$coreTables = [
|
||||
$this->prefix . 'projects',
|
||||
$this->prefix . 'currencies',
|
||||
$this->prefix . 'settings',
|
||||
$this->prefix . 'cost_plans',
|
||||
$this->prefix . 'measurements',
|
||||
@@ -335,18 +322,6 @@ final class SchemaManager
|
||||
$this->upgradeTargetOfferColumn();
|
||||
$applied[] = 'target_offer_column';
|
||||
}
|
||||
if (!$this->tableExists($this->prefix . 'currency_aliases')) {
|
||||
$this->ensureCurrencyAliasesTable();
|
||||
$applied[] = 'currency_aliases_table';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'currencies') && !$this->columnExists($this->prefix . 'currencies', 'is_crypto')) {
|
||||
$this->upgradeCurrenciesClassificationColumns();
|
||||
$applied[] = 'currency_classification';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'currencies')) {
|
||||
$this->ensureCurrencyForeignKeys();
|
||||
$applied[] = 'currency_foreign_keys';
|
||||
}
|
||||
if ($this->tableExists($this->prefix . 'miner_offers') && (
|
||||
!$this->columnExists($this->prefix . 'miner_offers', 'base_price_amount') ||
|
||||
!$this->columnExists($this->prefix . 'miner_offers', 'base_price_currency') ||
|
||||
@@ -788,36 +763,6 @@ final class SchemaManager
|
||||
$this->ensureMeasurementRatesTable();
|
||||
$this->ensurePayoutsTable();
|
||||
$this->ensureMinerTables();
|
||||
$this->ensureCurrencyAliasesTable();
|
||||
}
|
||||
|
||||
public function ensureCurrencyAliasesTable(): void
|
||||
{
|
||||
if ($this->tableExists($this->prefix . 'currency_aliases')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$table = $this->prefix . 'currency_aliases';
|
||||
$currencyTable = $this->prefix . 'currencies';
|
||||
$statements = $this->driver === 'pgsql'
|
||||
? [
|
||||
'CREATE TABLE IF NOT EXISTS ' . $table . ' (
|
||||
alias_code VARCHAR(10) PRIMARY KEY,
|
||||
currency_code VARCHAR(10) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_mining_currency_aliases_currency FOREIGN KEY (currency_code) REFERENCES ' . $currencyTable . '(code) ON DELETE CASCADE
|
||||
)',
|
||||
]
|
||||
: [
|
||||
'CREATE TABLE IF NOT EXISTS `' . $table . '` (
|
||||
alias_code VARCHAR(10) NOT NULL PRIMARY KEY,
|
||||
currency_code VARCHAR(10) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_mining_currency_aliases_currency FOREIGN KEY (currency_code) REFERENCES `' . $currencyTable . '`(code) ON DELETE CASCADE
|
||||
)',
|
||||
];
|
||||
|
||||
$this->executeUpgradeStatements($statements, 'Schema-Upgrade fuer Waehrungs-Aliase fehlgeschlagen.');
|
||||
}
|
||||
|
||||
public function ensureMeasurementRatesTable(): void
|
||||
@@ -1361,7 +1306,6 @@ final class SchemaManager
|
||||
{
|
||||
return [
|
||||
$this->prefix . 'projects',
|
||||
$this->prefix . 'currencies',
|
||||
$this->prefix . 'settings',
|
||||
$this->prefix . 'cost_plans',
|
||||
$this->prefix . 'measurements',
|
||||
@@ -1379,7 +1323,6 @@ final class SchemaManager
|
||||
$this->prefix . 'payouts',
|
||||
$this->prefix . 'miner_offers',
|
||||
$this->prefix . 'purchased_miners',
|
||||
$this->prefix . 'currency_aliases',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1392,8 +1335,6 @@ final class SchemaManager
|
||||
{
|
||||
return [
|
||||
$this->prefix . 'projects',
|
||||
$this->prefix . 'currencies',
|
||||
$this->prefix . 'currency_aliases',
|
||||
$this->prefix . 'settings',
|
||||
$this->prefix . 'cost_plans',
|
||||
$this->prefix . 'measurements',
|
||||
|
||||
Reference in New Issue
Block a user