isUserLoggedIn() || $this->response->getStatus() !== ServerStatus::OK) { return; } $db = new MySqlDatabase(); $fingerprint = new Fingerprint(null, $db); $this->response = new ApiJsonResponse(); try { if (!$this->hasUserPermission((int)$this->jsonBody->userId)) { return; } $fingerprint->setFingerprint((string)$this->jsonBody->fingerprint); $fingerprint->setUserId((int)$this->jsonBody->userId); if (!$db->hasTransaction()) { $db->startTransaction(); } $fingerprint->Save(); $qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint()); $qrCode->generate(); $qrCode->save(); $db->commit(); $this->response->setParameter('fingerprintId', $fingerprint->getFingerprintId()); } catch (QrCodeException $e) { $this->response->setStatus(ServerStatus::INTERNAL_ERROR); $this->response->setParameter('success', false); $this->response->setMessage('An error occured during QR code creation!'); } catch (Throwable $e) { $this->catchDatabaseException($e->getMessage(), $this->jsonBody); } } private function catchDatabaseException(string $message, object $json): void { $this->response->setParameter('success', false); if (substr_count($message, 'foreign key constraint fails') > 0) { $this->response->setMessage(sprintf('User with id %d doesn\'t exist!', $json->userId)); $this->response->setStatus(ServerStatus::NOT_FOUND); } elseif (substr_count($message, 'Duplicate entry') > 0) { $this->response->setMessage(sprintf('Fingerprint %s already exists!', $json->fingerprint)); $this->response->setStatus(ServerStatus::BAD_REQUEST); } else { $this->response->setMessage($message); $this->response->setStatus(ServerStatus::INTERNAL_ERROR); } } }