Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| TarFileFaker | |
100.00% |
34 / 34 |
|
100.00% |
5 / 5 |
8 | |
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% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| createEntry | |
100.00% |
17 / 17 |
|
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 | |
| 8 | /** |
| 9 | * Generates a ustar tar archive filled with random fake files, similar to the zip faker. |
| 10 | */ |
| 11 | final class TarFileFaker implements ApieFileFaker |
| 12 | { |
| 13 | private static bool $alreadyCreating = false; |
| 14 | |
| 15 | public function createOriginalFilename(Generator $faker): string |
| 16 | { |
| 17 | return $faker->word() . '.tar'; |
| 18 | } |
| 19 | |
| 20 | public function createMimeType(): string |
| 21 | { |
| 22 | return 'application/x-tar'; |
| 23 | } |
| 24 | |
| 25 | public static function isSupported(): bool |
| 26 | { |
| 27 | return !self::$alreadyCreating; |
| 28 | } |
| 29 | |
| 30 | /** @return resource */ |
| 31 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
| 32 | { |
| 33 | $resource = tmpfile(); |
| 34 | self::$alreadyCreating = true; |
| 35 | try { |
| 36 | for ($i = $faker->numberBetween(1, 10); $i > 0; $i--) { |
| 37 | /** @var SeededFile $file */ |
| 38 | $file = $faker->fakeClass(SeededFile::class); |
| 39 | $directory = $faker->boolean(30) ? $faker->word() . '/' : ''; |
| 40 | fwrite($resource, $this->createEntry( |
| 41 | $directory . $file->getClientFilename(), |
| 42 | (string) $file->getStream(), |
| 43 | $faker->dateTimeBetween('-2 years')->getTimestamp() |
| 44 | )); |
| 45 | } |
| 46 | } finally { |
| 47 | self::$alreadyCreating = false; |
| 48 | } |
| 49 | // end of archive is marked by 2 empty blocks. |
| 50 | fwrite($resource, str_repeat("\0", 1024)); |
| 51 | rewind($resource); |
| 52 | |
| 53 | return $resource; |
| 54 | } |
| 55 | |
| 56 | private function createEntry(string $name, string $data, int $modificationTime): string |
| 57 | { |
| 58 | $size = strlen($data); |
| 59 | $header = str_pad(substr($name, 0, 100), 100, "\0") |
| 60 | . sprintf('%07o', 0644) . "\0" |
| 61 | . sprintf('%07o', 1000) . "\0" |
| 62 | . sprintf('%07o', 1000) . "\0" |
| 63 | . sprintf('%011o', $size) . "\0" |
| 64 | . sprintf('%011o', $modificationTime) . "\0" |
| 65 | . ' ' |
| 66 | . '0' |
| 67 | . str_repeat("\0", 100) |
| 68 | . "ustar\0" . '00' |
| 69 | . str_pad('apie', 32, "\0") |
| 70 | . str_pad('apie', 32, "\0") |
| 71 | . str_repeat("\0", 8 + 8 + 155 + 12); |
| 72 | // the checksum is calculated while the checksum field itself contains spaces. |
| 73 | $checksum = sprintf('%06o', array_sum(unpack('C*', $header)) ?: 0) . "\0 "; |
| 74 | $header = substr_replace($header, $checksum, 148, 8); |
| 75 | |
| 76 | return $header . str_pad($data, (int) (ceil($size / 512) * 512), "\0"); |
| 77 | } |
| 78 | } |