From 0334c7022d361d7b9cbfe9ae7366840db6f084ad Mon Sep 17 00:00:00 2001 From: Mal <=> Date: Thu, 20 Aug 2020 16:53:54 +0200 Subject: [PATCH] Debugging of fingerprint deletion and QR code repair script added --- Makefile | 3 ++ .../FingerprintDeleteController.php | 9 ++++- backend/classes/core/QrCode.php | 33 ++++++++++++++++--- backend/scripts/repair.php | 31 +++++++++++++++++ 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 backend/scripts/repair.php diff --git a/Makefile b/Makefile index 9b8e9d4..5d91324 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +repair: + php backend/scripts/repair.php + setup: php backend/scripts/setup.php diff --git a/backend/classes/controller/FingerprintDeleteController.php b/backend/classes/controller/FingerprintDeleteController.php index 31ee4f0..4691324 100644 --- a/backend/classes/controller/FingerprintDeleteController.php +++ b/backend/classes/controller/FingerprintDeleteController.php @@ -24,8 +24,15 @@ final class FingerprintDeleteController extends AbstractController } try { - $fingerprint = new Fingerprint($this->fingerprintId); + $db = new MySqlDatabase(); + $db->startTransaction(); + + $fingerprint = new Fingerprint($this->fingerprintId, $db); + $qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint()); $fingerprint->Delete(); + $qrCode->delete(); + + $db->commit(); $this->response = new ApiJsonResponse(); } catch (Throwable $e) { diff --git a/backend/classes/core/QrCode.php b/backend/classes/core/QrCode.php index 4e41c9e..2e356fd 100644 --- a/backend/classes/core/QrCode.php +++ b/backend/classes/core/QrCode.php @@ -24,11 +24,7 @@ final class QrCode $returnCode = 0; - $path = substr(Setting::PATH_QR_CODES, -1) === '/' - ? Setting::PATH_QR_CODES - : Setting::PATH_QR_CODES . '/'; - - $filename = $path . $this->fingerprintId . '.svg'; + $filename = $this->getFilePath(); passthru( sprintf('mv %s %s', $this->temporaryFilename, $filename), @@ -42,6 +38,19 @@ final class QrCode } } + public function delete(): void + { + $filepath = $this->getFilePath(); + + if (!is_file($filepath)) { + throw new QrCodeException(sprintf('Qr code file %s not found!', $filepath)); + } + + if (!unlink($filepath)) { + throw new QrCodeException('Couldn\'t delete %s!', $filepath); + } + } + public function generate(): bool { $returnCode = 0; @@ -58,10 +67,24 @@ final class QrCode return !(bool)$returnCode; } + public function hasFile(): bool + { + return is_file($this->getFilePath()); + } + private function generateTemporaryFilename(): string { $hash = hash('md5', (new DateTime())->format('U') . $this->fingerprint); return sprintf('%s.svg', $hash); } + + private function getFilePath(): string + { + $path = substr(Setting::PATH_QR_CODES, -1) === '/' + ? Setting::PATH_QR_CODES + : Setting::PATH_QR_CODES . '/'; + + return $path . $this->fingerprintId . '.svg'; + } } \ No newline at end of file diff --git a/backend/scripts/repair.php b/backend/scripts/repair.php new file mode 100644 index 0000000..9bca3eb --- /dev/null +++ b/backend/scripts/repair.php @@ -0,0 +1,31 @@ +Select(Fingerprint::class, [Fingerprint::FIELD_ID]) as $record) { + $fingerprint = new Fingerprint((int)$record[Fingerprint::FIELD_ID], $db); + + $qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint()); + + if (!$qrCode->hasFile()) { + $countForMissing++; + + $qrCode->generate(); + $qrCode->save(); + + echo sprintf("\t=> Missing QR code for fingerprint %d generated.\n", $fingerprint->getFingerprintId()); + } +} + +echo $countForMissing === 0 + ? 'No missing QR codes found.' . PHP_EOL + : sprintf('%d missing QR code(s) fixed.', $countForMissing) . PHP_EOL; +