Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
DayOfEvent | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
get | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Helpers; |
6 | |
7 | class DayOfEvent |
8 | { |
9 | /** |
10 | * @return ?int The current day of the event. |
11 | * If `event_has_day0` is set to true in config, the first day of the event will be 0, else 1. |
12 | * Returns null if "event_start" is not set. |
13 | */ |
14 | public static function get(Carbon $date = null): int | null |
15 | { |
16 | if (!config('enable_day_of_event')) { |
17 | return null; |
18 | } |
19 | |
20 | $startOfEvent = config('event_start'); |
21 | |
22 | if (!$startOfEvent) { |
23 | return null; |
24 | } |
25 | |
26 | /** @var Carbon $startOfEvent */ |
27 | $startOfEvent = $startOfEvent->copy()->startOfDay(); |
28 | $date = $date ?: Carbon::now(); |
29 | |
30 | $now = $date->startOfDay(); |
31 | $diff = (int) $startOfEvent->diffInDays($now); |
32 | |
33 | if ($diff >= 0) { |
34 | // The first day of the event (diff 0) should be 1. |
35 | // The seconds day of the event (diff 1) should be 2. |
36 | // Add one day to the diff. |
37 | return $diff + 1; |
38 | } |
39 | |
40 | if (config('event_has_day0')) { |
41 | // One day before the event (-1 diff) should day 0. |
42 | // Two days before the event (-2 diff) should be -1. |
43 | // Add one day to the diff. |
44 | return $diff + 1; |
45 | } |
46 | |
47 | |
48 | // This is the remaining case where the diff is negative (before event). |
49 | // One day before the event (-1 diff) should be day -1. |
50 | // Two days before the event (-2 diff) should be day -2. |
51 | // Return as it is. |
52 | return $diff; |
53 | } |
54 | } |