Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ShiftsRenderer
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
5
 renderShiftCalendar
n/a
0 / 0
n/a
0 / 0
4
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Helpers;
6
7use Carbon\Carbon;
8use Engelsystem\Models\Shifts\Shift;
9use Engelsystem\Models\Shifts\ShiftEntry;
10use Engelsystem\ShiftCalendarRenderer;
11use Engelsystem\ShiftsFilter;
12use Illuminate\Support\Collection;
13
14class ShiftsRenderer
15{
16    /**
17     * This is glue code to force the legacy shift renderer to the new code
18     *
19     * @param Collection|Shift[] $shifts
20     */
21    public function render(Collection | array $shifts): string
22    {
23        /** @var array[] $neededAngelTypes */
24        $neededAngelTypes = [];
25        /** @var ShiftEntry[][] $shiftEntries */
26        $shiftEntries = [];
27
28        foreach ($shifts as $shift) {
29            $shiftEntries[$shift->id] = $shift->shiftEntries;
30
31            if (!$shift->schedule) {
32                $angelTypes = $shift->neededAngelTypes;
33            } else {
34                if ($shift->schedule->needed_from_shift_type) {
35                    $angelTypes = $shift->shiftType->neededAngelTypes;
36                } else {
37                    $angelTypes = $shift->location->neededAngelTypes;
38                }
39            }
40
41            $neededAngelTypes[$shift->id] = [];
42            foreach ($angelTypes as $nAngelType) {
43                $data = $nAngelType->toArray();
44                $data['id'] = $nAngelType->angelType->id;
45                $data['name'] = $nAngelType->angelType->name;
46                $data['restricted'] = $nAngelType->angelType->restricted;
47                $data['shift_self_signup'] = $nAngelType->angelType->shift_self_signup;
48                $neededAngelTypes[$shift->id][] = $data;
49            }
50        }
51
52        return $this->renderShiftCalendar($shifts, $neededAngelTypes, $shiftEntries);
53    }
54
55    /**
56     * @param Collection|Shift[] $shifts
57     * @param array[] $neededAngelTypes
58     * @param ShiftEntry[][] $shiftEntries
59     * @codeCoverageIgnore
60     */
61    protected function renderShiftCalendar(
62        array | Collection $shifts,
63        array $neededAngelTypes,
64        array $shiftEntries
65    ): string {
66        if (!$shifts instanceof Collection) {
67            $shifts = collect($shifts);
68        }
69
70        /** @var Carbon $start */
71        $start = $shifts->min('start');
72        /** @var Carbon $end */
73        $end = $shifts->max('end');
74
75        $shiftsFilter = new ShiftsFilter();
76        $shiftsFilter->setStartTime($start ? $start->timestamp : 0);
77        $shiftsFilter->setEndTime($end ? $end->timestamp : 0);
78
79        $renderer = new ShiftCalendarRenderer($shifts, $neededAngelTypes, $shiftEntries, $shiftsFilter);
80
81        return $renderer->render();
82    }
83}