Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
7 / 8 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ChainedExport | |
87.50% |
7 / 8 |
|
66.67% |
2 / 3 |
6.07 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| streamFromSheets | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getSupportedExtensions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Export; |
| 3 | |
| 4 | use Apie\Export\Lists\FileExtensionList; |
| 5 | use Psr\Http\Message\StreamInterface; |
| 6 | |
| 7 | final class ChainedExport implements ExportInterface |
| 8 | { |
| 9 | private array $exporters = []; |
| 10 | /** |
| 11 | * @param ExportInterface[] $exporters |
| 12 | */ |
| 13 | public function __construct(iterable $exporters) |
| 14 | { |
| 15 | foreach ($exporters as $exporter) { |
| 16 | foreach ($exporter->getSupportedExtensions() as $extension) { |
| 17 | $this->exporters[$extension->toNative()] = $exporter; |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | public function streamFromSheets(array $sheets, string $outputFilename): StreamInterface |
| 23 | { |
| 24 | $extension = pathinfo($outputFilename, PATHINFO_EXTENSION); |
| 25 | if (!isset($this->exporters[$extension])) { |
| 26 | throw new \InvalidArgumentException("No exporter found for extension: {$extension}"); |
| 27 | } |
| 28 | return $this->exporters[$extension]->streamFromSheets($sheets, $outputFilename); |
| 29 | } |
| 30 | |
| 31 | public function getSupportedExtensions(): FileExtensionList |
| 32 | { |
| 33 | return new FileExtensionList(array_keys($this->exporters)); |
| 34 | } |
| 35 | } |