Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
CompanyRolePlayAnalyticsResource
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 toArray
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Http\Resources\v2;
4
5use Illuminate\Http\Resources\Json\JsonResource;
6
7/**
8 * Pass-through resource for the corporate persona analytics payload.
9 *
10 * The data is already computed in the shape specified by the Phase 1 brief
11 * (`totals`, `timeseries`, `by_group`, `by_user`); this resource exists so
12 * the controller can uniformly return an API Resource and so the shape is
13 * stable even if raw payload keys need renaming at the model layer.
14 *
15 * Expected input shape on construction:
16 *   [
17 *     'totals' => ['sessions' => int, 'participants' => int, 'avg_score' => float,
18 *                  'median_score' => float, 'avg_duration' => float],
19 *     'timeseries' => [['week' => 'YYYY-Www', 'avg_score' => float, 'sessions' => int], ...],
20 *     'by_group' => [['group_id' => string, 'name' => string, 'participants' => int,
21 *                     'sessions' => int, 'avg_score' => float], ...],
22 *     'by_user' => [['user_id' => string, 'name' => string, 'sessions' => int,
23 *                    'avg_score' => float, 'last_session_at' => int, 'trend' => float[8]], ...],
24 *   ]
25 */
26class CompanyRolePlayAnalyticsResource extends JsonResource
27{
28    /**
29     * @return array<string, mixed>
30     */
31    public function toArray($request): array
32    {
33        /** @var array<string, mixed> $data */
34        $data = is_array($this->resource) ? $this->resource : [];
35
36        return [
37            'totals' => $data['totals'] ?? [
38                'sessions' => 0,
39                'participants' => 0,
40                'avg_score' => 0,
41                'median_score' => 0,
42                'avg_duration' => 0,
43            ],
44            'timeseries' => array_values($data['timeseries'] ?? []),
45            'by_group' => array_values($data['by_group'] ?? []),
46            'by_user' => array_values($data['by_user'] ?? []),
47        ];
48    }
49}