44
55namespace Itiden \Backup \Repositories ;
66
7- use Carbon \Carbon ;
7+ use Carbon \CarbonImmutable ;
88use Illuminate \Contracts \Filesystem \Filesystem ;
99use Illuminate \Filesystem \FilesystemAdapter ;
1010use Illuminate \Http \File as StreamableFile ;
1111use Illuminate \Support \Collection ;
1212use Illuminate \Support \Facades \Storage ;
1313use Illuminate \Support \Str ;
14+ use Itiden \Backup \Contracts \BackupNameResolver ;
1415use Itiden \Backup \Contracts \Repositories \BackupRepository ;
1516use Itiden \Backup \DataTransferObjects \BackupDto ;
1617use Itiden \Backup \Events \BackupDeleted ;
@@ -22,42 +23,30 @@ final class FileBackupRepository implements BackupRepository
2223 /** @var FilesystemAdapter */
2324 private Filesystem $ filesystem ;
2425
25- public function __construct ()
26- {
26+ public function __construct (
27+ private BackupNameResolver $ nameResolver ,
28+ ) {
2729 $ this ->path = config ('backup.destination.path ' );
2830 $ this ->filesystem = Storage::disk (config ('backup.destination.disk ' ));
2931 }
3032
31- private function makeFilename (string $ timestamp , string $ id ): string
32- {
33- return implode ('' , [
34- Str::slug (config ('app.name ' )),
35- '- ' ,
36- $ timestamp ,
37- '- ' ,
38- $ id ,
39- '.zip ' ,
40- ]);
41- }
42-
4333 public function all (): Collection
4434 {
45- return collect ($ this ->filesystem ->files ($ this ->path ))
35+ return collect ($ this ->filesystem ->allFiles ($ this ->path ))
4636 ->map (BackupDto::fromFile (...))
47- ->sortByDesc (' created_at ' );
37+ ->sortByDesc (fn ( BackupDto $ backup ) => $ backup -> created_at );
4838 }
4939
5040 public function add (string $ path ): BackupDto
5141 {
5242 $ this ->filesystem ->makeDirectory (path: $ this ->path );
5343
54- $ timestamp = (string ) Carbon::now ()->unix ();
5544 $ id = (string ) Str::ulid ();
5645
5746 $ this ->filesystem ->putFileAs (
5847 path: $ this ->path ,
5948 file: new StreamableFile ($ path ),
60- name: $ this ->makeFilename ( $ timestamp , $ id ),
49+ name: ( string ) str ( $ this ->nameResolver -> generateFilename (CarbonImmutable:: now () , $ id))-> finish ( ' .zip ' ),
6150 );
6251
6352 return $ this ->find ($ id );
0 commit comments