Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
22 / 22 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
ZipFileFaker | |
100.00% |
22 / 22 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createResource | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Apie\Faker\FileFakers; |
3 | |
4 | use Apie\Faker\Interfaces\ApieFileFaker; |
5 | use Apie\Faker\SeededFile; |
6 | use Faker\Generator; |
7 | use ZipStream\ZipStream; |
8 | |
9 | final class ZipFileFaker implements ApieFileFaker |
10 | { |
11 | private static bool $alreadyCreating = false; |
12 | public function createOriginalFilename(Generator $faker): string |
13 | { |
14 | return $faker->word() . '.zip'; |
15 | } |
16 | public function createMimeType(): string |
17 | { |
18 | return 'application/zip'; |
19 | } |
20 | public static function isSupported(): bool |
21 | { |
22 | return !self::$alreadyCreating; |
23 | } |
24 | /** @return resource */ |
25 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
26 | { |
27 | $resource = tmpfile(); |
28 | $zipstream = new ZipStream( |
29 | outputStream: $resource, |
30 | sendHttpHeaders: false, |
31 | outputName: $originalFilename, |
32 | contentType: $mimeType, |
33 | defaultEnableZeroHeader: true, |
34 | ); |
35 | $counter = $faker->numberBetween(1, 12); |
36 | self::$alreadyCreating = true; |
37 | try { |
38 | for ($i = 0; $i < $counter; $i++) { |
39 | /** @var SeededFile $file */ |
40 | $file = $faker->fakeClass(SeededFile::class); |
41 | $zipstream->addFileFromPsr7Stream( |
42 | fileName: $file->getClientFilename(), |
43 | stream: $file->getStream(), |
44 | ); |
45 | } |
46 | $zipstream->finish(); |
47 | } finally { |
48 | self::$alreadyCreating = false; |
49 | } |
50 | return $resource; |
51 | } |
52 | } |