77
88namespace Magmodules \Sooqr \Cron ;
99
10+ use Magento \Framework \App \Filesystem \DirectoryList ;
11+ use Magento \Framework \DB \Adapter \AdapterInterface ;
1012use Magento \Framework \App \ResourceConnection ;
1113use Magento \Framework \Exception \FileSystemException ;
1214use Magento \Framework \Filesystem \Driver \File ;
1921class Cleanup
2022{
2123
22- /**
23- * @var ConfigProvider
24- */
25- private $ configProvider ;
26- /**
27- * @var File
28- */
29- private $ file ;
30- /**
31- * @var ResourceConnection
32- */
33- private $ resourceConnection ;
34- /**
35- * @var LogRepository
36- */
37- private $ logger ;
24+ private ConfigProvider $ configProvider ;
25+ private File $ file ;
26+ private ResourceConnection $ resourceConnection ;
27+ private LogRepository $ logger ;
28+ private DirectoryList $ directoryList ;
3829
3930 /**
4031 * @param ConfigProvider $configProvider
4132 * @param File $file
4233 * @param LogRepository $logger
4334 * @param ResourceConnection $resourceConnection
35+ * @param DirectoryList $directoryList
4436 */
4537 public function __construct (
4638 ConfigProvider $ configProvider ,
4739 File $ file ,
4840 LogRepository $ logger ,
49- ResourceConnection $ resourceConnection
41+ ResourceConnection $ resourceConnection ,
42+ DirectoryList $ directoryList
5043 ) {
5144 $ this ->configProvider = $ configProvider ;
5245 $ this ->file = $ file ;
5346 $ this ->logger = $ logger ;
5447 $ this ->resourceConnection = $ resourceConnection ;
48+ $ this ->directoryList = $ directoryList ;
5549 }
5650
5751 /**
@@ -74,33 +68,77 @@ public function execute()
7468 private function removeFiles ($ offset )
7569 {
7670 $ connection = $ this ->resourceConnection ->getConnection ();
77- $ selectFiles = $ connection ->select ()->from (
78- $ this ->resourceConnection ->getTableName ('sooqr_feed ' ),
79- ['filename ' ]
80- )->where (
81- 'created_at < ? ' ,
82- date ("Y-m-d h:i:s " , strtotime ("- {$ offset } days " ))
83- )->where (
84- 'filename IS NOT NULL '
85- );
71+ $ filesToDelete = $ this ->getFilesToDelete ($ connection , $ offset );
8672
87- foreach ($ connection ->fetchCol ($ selectFiles ) as $ filename ) {
88- try {
89- if ($ this ->file ->isExists ($ filename )) {
90- $ this ->file ->deleteFile ($ filename );
91- }
92- } catch (FileSystemException $ exception ) {
93- $ this ->logger ->addDebugLog ('removeFiles ' , $ exception ->getMessage ());
94- }
73+ $ path = $ this ->directoryList ->getPath (DirectoryList::MEDIA ) . '/sooqr/data/ ' ;
74+
75+ foreach ($ filesToDelete as $ filename ) {
76+ $ this ->deleteFile ($ path , $ filename );
9577 }
9678
79+ $ this ->clearFileReferencesInDatabase ($ connection , $ offset );
80+
9781 $ connection ->update (
9882 $ this ->resourceConnection ->getTableName ('sooqr_feed ' ),
9983 ['filename ' => null , 'webhook_url ' => null ],
10084 ['created_at < ? ' => date ("Y-m-d h:i:s " , strtotime ("- {$ offset } days " ))]
10185 );
10286 }
10387
88+ /**
89+ * Clear file references in the database for outdated entries.
90+ *
91+ * @param AdapterInterface $connection
92+ * @param int $offset
93+ * @return void
94+ */
95+ private function clearFileReferencesInDatabase (AdapterInterface $ connection , int $ offset ): void
96+ {
97+ $ connection ->update (
98+ $ this ->resourceConnection ->getTableName ('sooqr_feed ' ),
99+ ['filename ' => null ],
100+ ['created_at < ? ' => $ this ->getPastDate ($ offset )]
101+ );
102+ }
103+
104+ /**
105+ * Delete a file and log any errors.
106+ *
107+ * @param string $path
108+ * @param string $filename
109+ * @return void
110+ */
111+ private function deleteFile (string $ path , string $ filename ): void
112+ {
113+ try {
114+ $ filename = $ path . $ filename . '.xml ' ;
115+ if ($ this ->file ->isExists ($ filename )) {
116+ $ this ->file ->deleteFile ($ filename );
117+ }
118+ } catch (FileSystemException $ exception ) {
119+ $ this ->logger ->addDebugLog ('cleanupFiles ' , $ exception ->getMessage ());
120+ }
121+ }
122+
123+ /**
124+ * Get files to delete based on the offset.
125+ *
126+ * @param AdapterInterface $connection
127+ * @param int $offset
128+ * @return array
129+ */
130+ private function getFilesToDelete (AdapterInterface $ connection , int $ offset ): array
131+ {
132+ $ tableName = $ this ->resourceConnection ->getTableName ('sooqr_feed ' );
133+
134+ return $ connection ->fetchCol (
135+ $ connection ->select ()
136+ ->from ($ tableName , ['filename ' ])
137+ ->where ('created_at < ? ' , $ this ->getPastDate ($ offset ))
138+ ->where ('filename IS NOT NULL ' )
139+ );
140+ }
141+
104142 /**
105143 * Remove database entries from 'sooqr_feed' table older than offset + 14-days
106144 *
@@ -115,4 +153,15 @@ private function removeEntries($offset)
115153 ['created_at < ? ' => date ("Y-m-d h:i:s " , strtotime ("- {$ offset } days " ))]
116154 );
117155 }
156+
157+ /**
158+ * Get a formatted past date based on the offset.
159+ *
160+ * @param int $offset
161+ * @return string
162+ */
163+ private function getPastDate (int $ offset ): string
164+ {
165+ return date ('Y-m-d H:i:s ' , strtotime ("- {$ offset } days " ));
166+ }
118167}
0 commit comments