Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportExportManager
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 all
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 definition
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 export
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 template
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 import
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Services\ImportExport;
4
5use Illuminate\Http\UploadedFile;
6use Maatwebsite\Excel\Facades\Excel;
7use Symfony\Component\HttpFoundation\BinaryFileResponse;
8
9class ImportExportManager
10{
11    /** @return array<string, Definition> */
12    public function all(): array
13    {
14        return collect(config('importexport', []))
15            ->map(fn ($class) => app($class))
16            ->all();
17    }
18
19    public function has(string $key): bool
20    {
21        return isset(config('importexport', [])[$key]);
22    }
23
24    public function definition(string $key): Definition
25    {
26        $class = config("importexport.{$key}");
27        abort_unless($class, 404, "No import/export definition for [{$key}]");
28
29        return app($class);
30    }
31
32    public function export(string $key, string $format, array $filters = []): BinaryFileResponse
33    {
34        $def = $this->definition($key);
35        $ext = $format === 'csv' ? 'csv' : 'xlsx';
36        $writer = $format === 'csv' ? \Maatwebsite\Excel\Excel::CSV : \Maatwebsite\Excel\Excel::XLSX;
37
38        return Excel::download(
39            new Exporter($def, $filters),
40            $key . '-' . now()->format('Y-m-d') . '.' . $ext,
41            $writer
42        );
43    }
44
45    public function template(string $key): BinaryFileResponse
46    {
47        $def = $this->definition($key);
48
49        return Excel::download(
50            new TemplateExport($def),
51            $key . '-import-template.xlsx'
52        );
53    }
54
55    /**
56     * Run an import from an uploaded file. dryRun=true validates + reports
57     * without saving. $file is an UploadedFile (kept with its extension so
58     * the reader can detect the format).
59     *
60     * @return array{processed:int,created:int,updated:int,skipped:int,errors:array}
61     */
62    public function import(string $key, UploadedFile $file, bool $dryRun = true): array
63    {
64        $def = $this->definition($key);
65        $importer = new Importer($def);
66
67        $collection = Excel::toCollection(new HeadingImport, $file)->first() ?? collect();
68
69        return $importer->run($collection, $dryRun);
70    }
71}