3636 */
3737readonly class Backup
3838{
39+ /** @var BackupRepository */
40+ private BackupRepository $ repository ;
41+
3942 /**
4043 * Constructor.
4144 */
4245 public function __construct (
4346 private Configuration $ configuration ,
4447 private DatabaseHelper $ databaseHelper ,
4548 ) {
49+ $ this ->repository = new BackupRepository ($ this ->configuration );
4650 }
4751
4852 /**
@@ -58,17 +62,8 @@ public function createBackup(string $backupType, string $backupFile): string
5862 $ authKey = sodium_crypto_auth_keygen ();
5963 $ authCode = sodium_crypto_auth ($ backupFile , $ authKey );
6064
61- $ query = sprintf (
62- "INSERT INTO %sfaqbackup (id, filename, authkey, authcode, created) VALUES (%d, '%s', '%s', '%s', '%s') " ,
63- Database::getTablePrefix (),
64- $ this ->configuration ->getDb ()->nextId (Database::getTablePrefix () . 'faqbackup ' , 'id ' ),
65- $ this ->configuration ->getDb ()->escape ($ fileName ),
66- $ this ->configuration ->getDb ()->escape (sodium_bin2hex ($ authKey )),
67- $ this ->configuration ->getDb ()->escape (sodium_bin2hex ($ authCode )),
68- $ backupDate ,
69- );
70-
71- $ this ->configuration ->getDb ()->query ($ query );
65+ // persist backup metadata via repository
66+ $ this ->getRepository ()->add ($ fileName , sodium_bin2hex ($ authKey ), sodium_bin2hex ($ authCode ), $ backupDate );
7267
7368 return $ fileName ;
7469 }
@@ -78,17 +73,8 @@ public function createBackup(string $backupType, string $backupFile): string
7873 */
7974 public function verifyBackup (string $ backup , string $ backupFileName ): bool
8075 {
81- $ query = sprintf (
82- "SELECT id, filename, authkey, authcode, created FROM %sfaqbackup WHERE filename = '%s' " ,
83- Database::getTablePrefix (),
84- $ this ->configuration ->getDb ()->escape ($ backupFileName ),
85- );
86-
87- $ result = $ this ->configuration ->getDb ()->query ($ query );
88-
89- if ($ this ->configuration ->getDb ()->numRows ($ result ) > 0 ) {
90- $ row = $ this ->configuration ->getDb ()->fetchObject ($ result );
91-
76+ $ row = $ this ->getRepository ()->findByFilename ($ backupFileName );
77+ if ($ row !== null ) {
9278 return sodium_crypto_auth_verify (
9379 sodium_hex2bin ((string ) $ row ->authcode ),
9480 $ backup ,
@@ -101,21 +87,29 @@ public function verifyBackup(string $backup, string $backupFileName): bool
10187
10288 public function generateBackupQueries (string $ tableNames ): string
10389 {
104- $ backup = implode ("\r\n" , $ this ->getBackupHeader ($ tableNames ));
105-
106- foreach (explode (' ' , $ tableNames ) as $ table ) {
107- if ('' !== $ table ) {
108- $ backup .= implode ("\r\n" , $ this ->databaseHelper ->buildInsertQueries (
109- 'SELECT * FROM ' . $ table ,
110- $ table ,
111- ));
90+ $ backup = implode (
91+ separator: "\r\n" ,
92+ array: $ this ->getBackupHeader ($ tableNames ),
93+ );
94+
95+ foreach (explode (
96+ separator: ' ' ,
97+ string: $ tableNames ,
98+ ) as $ tableName ) {
99+ if ('' !== $ tableName ) {
100+ $ backup .= implode (
101+ separator: "\r\n" ,
102+ array: $ this ->databaseHelper ->buildInsertQueries ('SELECT * FROM ' . $ tableName , $ tableName ),
103+ );
112104 }
113105 }
114106
115107 return $ backup ;
116108 }
117109
118- public function getBackupTableNames (BackupType $ backupType ): string
110+ /**
111+ * @throws \Exception
112+ */ public function getBackupTableNames (BackupType $ backupType ): string
119113 {
120114 $ tables = $ this ->configuration ->getDb ()->getTableNames (Database::getTablePrefix ());
121115 $ tableNames = '' ;
@@ -146,6 +140,8 @@ public function getBackupTableNames(BackupType $backupType): string
146140 }
147141
148142 break ;
143+ case BackupType::BACKUP_TYPE_CONTENT :
144+ throw new \Exception (message: 'To be implemented ' );
149145 }
150146
151147 return $ tableNames ;
@@ -176,8 +172,8 @@ public function createContentFolderBackup(): string
176172 $ zipFile = PMF_ROOT_DIR . DIRECTORY_SEPARATOR . 'content.zip ' ;
177173
178174 $ zipArchive = new ZipArchive ();
179- if ($ zipArchive ->open ($ zipFile , ZipArchive::CREATE ) !== true ) {
180- throw new Exception ('Error while creating ZipArchive ' );
175+ if (! $ zipArchive ->open ($ zipFile , ZipArchive::CREATE )) {
176+ throw new Exception (message: 'Error while creating ZipArchive ' );
181177 }
182178
183179 $ files = new RecursiveIteratorIterator (
@@ -197,4 +193,9 @@ public function createContentFolderBackup(): string
197193
198194 return $ zipFile ;
199195 }
196+
197+ private function getRepository (): BackupRepository
198+ {
199+ return $ this ->repository ;
200+ }
200201}
0 commit comments