Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| ReportService | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| all | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| visible | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| grouped | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| run | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\Reporting; |
| 4 | |
| 5 | use App\Reports\Report; |
| 6 | use Illuminate\Support\Collection; |
| 7 | |
| 8 | /** |
| 9 | * Resolves config/reports.php into Report instances, filters them down to |
| 10 | * whatever the current feature flags allow, and runs one end to end |
| 11 | * (filters → rows → summary). See App\Reports\Report for the contract every |
| 12 | * report implements, and App\Http\Controllers\admin\ReportsController for |
| 13 | * the routes/export that consume this. |
| 14 | */ |
| 15 | class ReportService |
| 16 | { |
| 17 | /** @return array<string, Report> */ |
| 18 | public function all(): array |
| 19 | { |
| 20 | return collect(config('reports', [])) |
| 21 | ->mapWithKeys(fn ($class, $key) => [$key => app($class)]) |
| 22 | ->all(); |
| 23 | } |
| 24 | |
| 25 | /** Every registered report whose feature() (if any) is currently on. */ |
| 26 | public function visible(): Collection |
| 27 | { |
| 28 | return collect($this->all())->filter(fn (Report $r) => ! $r->feature() || feature($r->feature())); |
| 29 | } |
| 30 | |
| 31 | /** Visible reports grouped by their group(), in registry order. */ |
| 32 | public function grouped(): Collection |
| 33 | { |
| 34 | return $this->visible()->groupBy(fn (Report $r) => $r->group()); |
| 35 | } |
| 36 | |
| 37 | public function find(string $key): Report |
| 38 | { |
| 39 | $all = $this->all(); |
| 40 | |
| 41 | abort_unless(isset($all[$key]), 404, 'Unknown report.'); |
| 42 | |
| 43 | $report = $all[$key]; |
| 44 | |
| 45 | abort_if($report->feature() && ! feature($report->feature()), 404); |
| 46 | |
| 47 | return $report; |
| 48 | } |
| 49 | |
| 50 | /** @return array{report: Report, filters: array, rows: Collection, summary: array} */ |
| 51 | public function run(string $key, array $input): array |
| 52 | { |
| 53 | $report = $this->find($key); |
| 54 | $filters = $report->resolveFilters($input); |
| 55 | $rows = $report->rows($filters); |
| 56 | |
| 57 | return [ |
| 58 | 'report' => $report, |
| 59 | 'filters' => $filters, |
| 60 | 'rows' => $rows, |
| 61 | 'summary' => $report->summary($rows, $filters), |
| 62 | ]; |
| 63 | } |
| 64 | } |