turnstile-remaster/src/Migration/Migration1700000000AddCloudflareTurnstileCaptcha.php

97 lines
3.7 KiB
PHP

<?php declare(strict_types=1);
namespace TurnstileReMaster\Migration;
use Doctrine\DBAL\Connection;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Migration\MigrationStep;
use Shopware\Core\Framework\Uuid\Uuid;
class Migration1700000000AddCloudflareTurnstileCaptcha extends MigrationStep
{
// Der System-Config Key für die Captchas
private const CONFIG_KEY = 'core.basicInformation.activeCaptchasV2';
// Der eindeutige Key für CloudFlare Captcha innerhalb der Konfiguration
private const CLOUDFLARE_CAPTCHA_KEY = 'cloudFlareTurnstile';
// generischer Timestamp um nicht mit anderen Migrationen zu kollidieren
public function getCreationTimestamp(): int
{
return 1700000000;
}
private function getCloudflareDefaultConfig(): array
{
return [
'name' => self::CLOUDFLARE_CAPTCHA_KEY, // Technischer Name für die Identifikation
'isActive' => false, // Standardmäßig deaktiviert
'config' => [
'siteKey' => '',
'secretKey' => '',
],
];
}
public function update(Connection $connection): void
{
$configId = $connection->fetchOne(
'SELECT id FROM system_config WHERE configuration_key = :key AND sales_channel_id IS NULL LIMIT 1',
['key' => self::CONFIG_KEY]
);
if ($configId === false) {
// Fallback: Eintrag existiert nicht -> Erstelle ihn mit CloudFlare Captcha
// Normalerweise existiert der Core-Eintrag bereits schon.
$connection->insert('system_config', [
'id' => Uuid::randomBytes(),
'configuration_key' => self::CONFIG_KEY,
'configuration_value' => json_encode(['_value' => [self::CLOUDFLARE_CAPTCHA_KEY => $this->getCloudflareDefaultConfig()]]),
'sales_channel_id' => null,
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
]);
return;
}
// Eintrag existiert, hole aktuellen Wert
$currentValueJson = $connection->fetchOne(
'SELECT configuration_value FROM system_config WHERE id = :id',
['id' => $configId]
);
$configValue = ['_value' => []]; // Standard, falls JSON leer oder ungültig
if (!empty($currentValueJson)) {
$decoded = json_decode($currentValueJson, true);
if (json_last_error() === JSON_ERROR_NONE && isset($decoded['_value']) && is_array($decoded['_value'])) {
$configValue = $decoded;
} else {
// Log oder Fehlerbehandlung, wenn JSON-Struktur kaputt ist.
// Für die Migration wird mit einem leeren Array fortgesetzt oder abgebrochen.
error_log('TurnstileReMaster Migration: Invalid JSON structure found for ' . self::CONFIG_KEY);
// return; // Oder Abbruch, wenn es zu riskant ist
}
}
// Prüfen, ob CloudFlare Captcha schon da ist
if (isset($configValue['_value'][self::CLOUDFLARE_CAPTCHA_KEY])) {
return; // Nichts zu tun
}
// CloudFlare Captcha hinzufügen
$configValue['_value'][self::CLOUDFLARE_CAPTCHA_KEY] = $this->getCloudflareDefaultConfig();
// Aktualisierte Konfiguration speichern
$connection->update(
'system_config',
[
'configuration_value' => json_encode($configValue),
'updated_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
],
['id' => $configId]
);
}
public function updateDestructive(Connection $connection): void
{
// Nichts zu tun hier
}
}