@@ -30,17 +30,31 @@ public function image(string $key): Factory
30
30
});
31
31
}
32
32
33
- public function withFile (? string $ fileName = null )
33
+ public function file ( string $ key ): Factory
34
34
{
35
- return $ this ->state (function (array $ attributes ) use ($ fileName ) {
36
- $ fileName = $ fileName ?: fake ()->slug () . '.jpg ' ;
37
- $ path = $ this ->getRandomFixtureImagePath ();
35
+ return $ this
36
+ ->state (function (array $ attributes ) use ($ key ) {
37
+ return [
38
+ 'model_key ' => $ key ,
39
+ 'file_name ' => sprintf ('data/files/%s.jpg ' , $ this ->faker ->unique ()->slug ()),
40
+ 'mime_type ' => 'image/jpeg ' ,
41
+ 'disk ' => 'local ' ,
42
+ 'size ' => $ this ->faker ->numberBetween (100 , 100000 ),
43
+ ];
44
+ });
45
+ }
46
+
47
+ public function withFile (?string $ fileName = null , string $ type ="image " )
48
+ {
49
+ return $ this ->state (function (array $ attributes ) use ($ fileName ,$ type ) {
50
+ $ fileName = $ fileName ?: fake ()->slug () . ($ type ==='image ' ?'.jpg ' :'.pdf ' );
51
+ $ path = ($ type ==="image " ? $ this ->getRandomFixtureImagePath () : $ this ->getRandomFixtureDocumentPath ());
38
52
39
53
Storage::disk ('local ' )
40
- ->put ("/data/medias/ $ fileName " , file_get_contents ($ path ));
54
+ ->put ("/data/ " .( $ type === ' image ' ? ' medias ' : ' files ' ). " / $ fileName " , file_get_contents ($ path ));
41
55
42
56
return [
43
- 'file_name ' => "data/medias/ $ fileName " ,
57
+ 'file_name ' => "data/ " .( $ type === ' image ' ? ' medias ' : ' files ' ). " / $ fileName " ,
44
58
];
45
59
});
46
60
}
@@ -54,4 +68,14 @@ private function getRandomFixtureImagePath(): string
54
68
)
55
69
);
56
70
}
71
+
72
+ private function getRandomFixtureDocumentPath (): string
73
+ {
74
+ return base_path (
75
+ sprintf (
76
+ 'vendor/code16/ozu-client/database/fixtures/documents/%s.pdf ' ,
77
+ rand (1 , 26 )
78
+ )
79
+ );
80
+ }
57
81
}
0 commit comments