@@ -63,16 +63,31 @@ public function handle(): int
6363 $ totalTested = 0 ;
6464 $ totalPassed = 0 ;
6565 $ offset = 0 ;
66+ $ exportFh = null ;
67+ if ($ exportPath !== '' ) {
68+ $ path = $ this ->resolvePath ($ exportPath );
69+ $ dir = dirname ($ path );
70+ if (! is_dir ($ dir ) && ! @mkdir ($ dir , 0775 , true ) && ! is_dir ($ dir )) {
71+ $ this ->error ("Failed to create export directory: {$ dir }" );
72+ return self ::FAILURE ;
73+ }
74+ $ exportFh = @fopen ($ path , 'wb ' );
75+ if (! $ exportFh ) {
76+ $ this ->error ("Failed to open export file: {$ path }" );
77+ return self ::FAILURE ;
78+ }
79+ fputcsv ($ exportFh , ['id ' , 'type ' , 'edition ' , 'user_id ' , 'email ' , 'name_for_certificate ' , 'error ' ]);
80+ }
6681
6782 while ($ offset < $ totalToTest ) {
83+ $ failuresBeforeBatch = count ($ allFailures );
6884 $ take = min ($ batchSize , $ totalToTest - $ offset );
6985 $ query = (clone $ baseQuery )->offset ($ offset )->limit ($ take );
7086 $ rows = $ query ->get ();
7187 if ($ rows ->isEmpty ()) {
7288 break ;
7389 }
7490
75- $ batchNum = (int ) floor ($ offset / $ batchSize ) + 1 ;
7691 $ bar = $ this ->output ->createProgressBar ($ rows ->count ());
7792 $ bar ->setFormat (" Batch %current%/%max% [%bar%] %percent:3s%% — failures this batch: " );
7893 $ bar ->start ();
@@ -125,6 +140,21 @@ public function handle(): int
125140 $ bar ->finish ();
126141 $ totalTested += $ rows ->count ();
127142 $ this ->line (" {$ batchFailures } | Total: {$ totalTested }/ {$ totalToTest } tested, " . count ($ allFailures ) . " failures. " );
143+ if ($ exportFh !== null ) {
144+ $ batchFailureRows = array_slice ($ allFailures , $ failuresBeforeBatch );
145+ foreach ($ batchFailureRows as $ row ) {
146+ fputcsv ($ exportFh , [
147+ $ row ['id ' ],
148+ $ row ['type ' ],
149+ $ edition ,
150+ $ row ['user_id ' ],
151+ $ row ['email ' ],
152+ $ row ['name ' ],
153+ $ row ['error ' ],
154+ ]);
155+ }
156+ fflush ($ exportFh );
157+ }
128158 $ offset += $ rows ->count ();
129159 }
130160
@@ -139,31 +169,9 @@ public function handle(): int
139169 }
140170 }
141171
142- if ($ exportPath !== '' ) {
172+ if ($ exportFh !== null ) {
173+ fclose ($ exportFh );
143174 $ path = $ this ->resolvePath ($ exportPath );
144- $ dir = dirname ($ path );
145- if (! is_dir ($ dir ) && ! @mkdir ($ dir , 0775 , true ) && ! is_dir ($ dir )) {
146- $ this ->error ("Failed to create export directory: {$ dir }" );
147- return self ::FAILURE ;
148- }
149- $ fh = @fopen ($ path , 'wb ' );
150- if (! $ fh ) {
151- $ this ->error ("Failed to open export file: {$ path }" );
152- return self ::FAILURE ;
153- }
154- fputcsv ($ fh , ['id ' , 'type ' , 'edition ' , 'user_id ' , 'email ' , 'name_for_certificate ' , 'error ' ]);
155- foreach ($ allFailures as $ row ) {
156- fputcsv ($ fh , [
157- $ row ['id ' ],
158- $ row ['type ' ],
159- $ edition ,
160- $ row ['user_id ' ],
161- $ row ['email ' ],
162- $ row ['name ' ],
163- $ row ['error ' ],
164- ]);
165- }
166- fclose ($ fh );
167175 $ this ->info (empty ($ allFailures ) ? "Exported (no failures): {$ path }" : "Exported failures only: {$ path }" );
168176 }
169177
0 commit comments