44
55use Dotenv \Dotenv ;
66
7+ use Exception ;
8+ use PDO ;
9+ use PDOException ;
10+
711/**
812 * Class PersistentState
913 *
1721 * @package VerifierServer
1822 */
1923class PersistentState {
20- private \ PDO $ pdo ;
24+ private PDO $ pdo ;
2125 private array $ verifyList ;
2226
2327 public function __construct (
@@ -46,26 +50,26 @@ public function __construct(
4650 * - discord: TEXT
4751 * - create_time: TEXT
4852 *
49- * @throws \ PDOException if the table creation fails.
53+ * @throws PDOException if the table creation fails.
5054 */
5155 private function initializeDatabase (): void
5256 {
5357 $ env = self ::loadEnvConfig ();
54- $ this ->pdo = new \ PDO (
58+ $ this ->pdo = new PDO (
5559 $ env ['DB_DSN ' ],
5660 $ env ['DB_USERNAME ' ],
5761 $ env ['DB_PASSWORD ' ],
5862 isset ($ env ['DB_OPTIONS ' ]) ? json_decode ($ env ['DB_OPTIONS ' ], true ) : null
5963 );
60- if (strpos ($ this ->pdo ->getAttribute (\ PDO ::ATTR_DRIVER_NAME ), 'mysql ' ) !== false ) {
64+ if (strpos ($ this ->pdo ->getAttribute (PDO ::ATTR_DRIVER_NAME ), 'mysql ' ) !== false ) {
6165 if ($ this ->pdo ->exec ("CREATE TABLE IF NOT EXISTS verify_list (
6266 id INT AUTO_INCREMENT PRIMARY KEY,
6367 ss13 VARCHAR(255),
6468 discord VARCHAR(255),
6569 create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
6670 ) " ) === false ) {
6771 $ errorInfo = $ this ->pdo ->errorInfo ();
68- throw new \ PDOException ("Failed to create table: " . implode (", " , $ errorInfo ));
72+ throw new PDOException ("Failed to create table: " . implode (", " , $ errorInfo ));
6973 }
7074 } else {
7175 if ($ this ->pdo ->exec ("CREATE TABLE IF NOT EXISTS verify_list (
@@ -75,7 +79,7 @@ private function initializeDatabase(): void
7579 create_time TEXT
7680 ) " ) === false ) {
7781 $ errorInfo = $ this ->pdo ->errorInfo ();
78- throw new \ PDOException ("Failed to create table: " . implode (", " , $ errorInfo ));
82+ throw new PDOException ("Failed to create table: " . implode (", " , $ errorInfo ));
7983 }
8084 }
8185 }
@@ -88,7 +92,7 @@ private function initializeDatabase(): void
8892 *
8993 * @return array The verification list.
9094 *
91- * @throws \ PDOException If there is an error executing the query or fetching the data from the database.
95+ * @throws PDOException If there is an error executing the query or fetching the data from the database.
9296 */
9397 public function getVerifyList (bool $ getLocalCache = false ): array
9498 {
@@ -100,12 +104,12 @@ public function getVerifyList(bool $getLocalCache = false): array
100104 $ stmt = $ this ->pdo ->query ("SELECT * FROM verify_list " );
101105 if ($ stmt === false ) {
102106 $ errorInfo = $ this ->pdo ->errorInfo ();
103- throw new \ PDOException ("Failed to execute query: " . implode (", " , $ errorInfo ));
107+ throw new PDOException ("Failed to execute query: " . implode (", " , $ errorInfo ));
104108 }
105- $ result = $ stmt ->fetchAll (\ PDO ::FETCH_ASSOC );
109+ $ result = $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
106110 if ($ result === false ) {
107111 $ errorInfo = $ this ->pdo ->errorInfo ();
108- throw new \ PDOException ("Failed to fetch data: " . implode (", " , $ errorInfo ));
112+ throw new PDOException ("Failed to fetch data: " . implode (", " , $ errorInfo ));
109113 }
110114 return $ this ->verifyList = $ result ;
111115 }
@@ -121,21 +125,21 @@ public function getVerifyList(bool $getLocalCache = false): array
121125 * with keys 'ss13', 'discord', and 'create_time'.
122126 * @param bool $write Whether to write the list to the database. Default is true.
123127 *
124- * @throws \ PDOException If there is an error deleting from the verify_list table, preparing the insert statement,
128+ * @throws PDOException If there is an error deleting from the verify_list table, preparing the insert statement,
125129 * or executing the insert statement.
126130 */
127131 public function setVerifyList (array $ list , bool $ write = true ): void
128132 {
129133 if ($ write && $ this ->storageType !== 'filesystem ' ) {
130134 if ($ this ->pdo ->exec ("DELETE FROM verify_list " ) === false ) {
131- throw new \ PDOException ("Failed to delete from verify_list: " . implode (", " , $ this ->pdo ->errorInfo ()));
135+ throw new PDOException ("Failed to delete from verify_list: " . implode (", " , $ this ->pdo ->errorInfo ()));
132136 }
133137 $ stmt = $ this ->pdo ->prepare ("INSERT INTO verify_list (ss13, discord, create_time) VALUES (:ss13, :discord, :create_time) " );
134138 if ($ stmt === false ) {
135- throw new \ PDOException ("Failed to prepare statement. " );
139+ throw new PDOException ("Failed to prepare statement. " );
136140 }
137141 foreach ($ list as $ item ) if (!$ stmt ->execute ($ item )) {
138- throw new \ PDOException ("Failed to execute statement. " );
142+ throw new PDOException ("Failed to execute statement. " );
139143 }
140144 }
141145 $ this ->verifyList = $ list ;
@@ -230,7 +234,7 @@ public static function loadEnvConfig(): array
230234 * If the file cannot be read, it throws an exception.
231235 *
232236 * @return array|null The decoded JSON data as an associative array, or an empty array if the file is empty or invalid.
233- * @throws \ Exception If the file cannot be read.
237+ * @throws Exception If the file cannot be read.
234238 */
235239 public static function loadVerifyFile (string $ json_path ): ?array
236240 {
@@ -243,7 +247,7 @@ public static function loadVerifyFile(string $json_path): ?array
243247 }
244248 $ data = file_get_contents ($ json_path );
245249 if ($ data === false ) {
246- throw new \ Exception ("Failed to read {$ json_path }" );
250+ throw new Exception ("Failed to read {$ json_path }" );
247251 }
248252 return json_decode ($ data , true ) ?: [];
249253 }
0 commit comments