. */ namespace OrangeHRM\Installer\Util\Dto; use Doctrine\DBAL\Connection as DBALConnection; use InvalidArgumentException; use OrangeHRM\Installer\Exception\SystemCheckException; use OrangeHRM\Installer\Util\Logger; use OrangeHRM\Installer\Util\Messages; use OrangeHRM\Installer\Util\StateContainer; use OrangeHRM\Installer\Util\SystemCheck; use Throwable; class DatabaseConnectionWrapper { public const ERROR_CODE_ACCESS_DENIED = 1045; public const ERROR_CODE_INVALID_HOST_PORT = 2002; public const ERROR_CODE_DATABASE_NOT_EXISTS = 1049; private ?DBALConnection $conn; private ?Throwable $e; public function __construct(?DBALConnection $conn, ?Throwable $e = null) { $this->conn = $conn; $this->e = $e; } /** * @return bool */ public function hasError(): bool { return $this->e instanceof Throwable; } public function getErrorMessage(): ?string { if (!$this->hasError()) { return null; } $errorMessage = $this->e->getMessage(); $errorCode = $this->e->getCode(); if ($this->e instanceof SystemCheckException) { return $this->e->getMessage(); } if ($errorCode === self::ERROR_CODE_INVALID_HOST_PORT) { $dbInfo = StateContainer::getInstance()->getDbInfo(); $dbHost = $dbInfo[StateContainer::DB_HOST]; $dbPort = $dbInfo[StateContainer::DB_PORT]; $message = "The MySQL server isn't running on `$dbHost:$dbPort`. " . Messages::ERROR_MESSAGE_INVALID_HOST_PORT; } elseif ($errorCode === self::ERROR_CODE_ACCESS_DENIED) { $message = Messages::ERROR_MESSAGE_ACCESS_DENIED; } elseif ($errorCode === self::ERROR_CODE_DATABASE_NOT_EXISTS) { $message = 'Database Not Exist'; } else { $message = $errorMessage . ' ' . Messages::ERROR_MESSAGE_REFER_LOG_FOR_MORE; } return $message; } /** * @return Throwable|null */ public function getThrowable(): ?Throwable { return $this->e; } /** * @param callable $dbalConnectionGetter * @return self */ public static function establishConnection(callable $dbalConnectionGetter): self { $systemCheck = new SystemCheck(); if (!$systemCheck->checkPDOExtensionEnabled()) { return new self(null, SystemCheckException::notEnabledPDOExtension()); } if (!$systemCheck->checkPDOMySqlExtensionEnabled()) { return new self(null, SystemCheckException::notEnabledPDOMySQLDriver()); } try { $conn = $dbalConnectionGetter(); if ($conn instanceof DBALConnection) { $conn->connect(); return new self($conn); } throw new InvalidArgumentException('Invalid callback provided'); } catch (Throwable $e) { Logger::getLogger()->error($e->getMessage()); Logger::getLogger()->error($e->getTraceAsString()); if ($e instanceof InvalidArgumentException) { throw $e; } return new self(null, $e); } } }