-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
VaultLifeMetricsViewHelper.php
138 lines (120 loc) · 4.88 KB
/
VaultLifeMetricsViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace App\Domains\Vault\ManageLifeMetrics\Web\ViewHelpers;
use App\Helpers\DateHelper;
use App\Models\Contact;
use App\Models\LifeMetric;
use App\Models\User;
use App\Models\Vault;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
/**
* Sorry if you need to read this code. I'm not proud of it.
* It's messy, it's inelegant.
* I don't like to deal with data like this.
*/
class VaultLifeMetricsViewHelper
{
public static function data(Vault $vault, User $user, int $year): array
{
$lifeMetrics = $vault->lifeMetrics;
$contact = $user->getContactInVault($vault);
$lifeMetricsCollection = $lifeMetrics->map(fn (LifeMetric $lifeMetric) => self::dto($lifeMetric, $year, $contact));
return [
'data' => $lifeMetricsCollection,
'url' => [
'store' => route('vault.life_metrics.store', [
'vault' => $vault->id,
]),
],
];
}
public static function dto(LifeMetric $lifeMetric, int $year, Contact $contact): array
{
$events = $contact->lifeMetrics()
->where('life_metric_id', $lifeMetric->id)
->get();
// get all the events for the given year
$monthlyEvents = $events->filter(fn ($lifeMetricEvent) => Carbon::parse($lifeMetricEvent->pivot->created_at)->year === $year
);
$maxNumberOfEvents = 0;
$eventsInMonthCollection = collect();
for ($month = 1; $month < 13; $month++) {
$eventsCounter = 0;
foreach ($monthlyEvents as $monthlyEvent) {
if (CarbonImmutable::parse($monthlyEvent->pivot->created_at)->month === $month) {
$eventsCounter++;
}
}
if ($maxNumberOfEvents < $eventsCounter) {
$maxNumberOfEvents = $eventsCounter;
}
$date = CarbonImmutable::now()->day(1)->month($month);
$eventsInMonthCollection->push([
'id' => $month,
'friendly_name' => DateHelper::formatMonthNumber($date),
'events' => $eventsCounter,
]);
}
return [
'id' => $lifeMetric->id,
'incremented' => false,
'show_graph' => false,
'label' => $lifeMetric->label,
'stats' => self::stats($lifeMetric, $contact),
'years' => self::years($lifeMetric, $contact),
'months' => $eventsInMonthCollection,
'max_number_of_events' => $maxNumberOfEvents,
'url' => [
'store' => route('vault.life_metrics.contact.store', [
'vault' => $contact->vault->id,
'metric' => $lifeMetric->id,
]),
'update' => route('vault.life_metrics.update', [
'vault' => $contact->vault->id,
'metric' => $lifeMetric->id,
]),
'destroy' => route('vault.life_metrics.destroy', [
'vault' => $contact->vault->id,
'metric' => $lifeMetric->id,
]),
],
];
}
public static function years(LifeMetric $lifeMetric, Contact $contact): Collection
{
$events = $contact->lifeMetrics()
->where('life_metric_id', $lifeMetric->id)
->get();
$yearsCollection = $events->map(fn (LifeMetric $lifeMetricEvent) => [
'year' => Carbon::parse($lifeMetricEvent->pivot->created_at)->year,
]);
return $yearsCollection->unique('year')->sortByDesc('year')->values();
}
public static function stats(LifeMetric $lifeMetric, Contact $contact): array
{
// get all the events of the current week
$weeklyEvents = $contact->lifeMetrics()
->where('life_metric_id', $lifeMetric->id)
->wherePivot('created_at', '<=', CarbonImmutable::now()->endOfWeek())
->wherePivot('created_at', '>=', CarbonImmutable::now()->startOfWeek())
->count();
// get all the events of the current month
$monthlyEvents = $contact->lifeMetrics()
->where('life_metric_id', $lifeMetric->id)
->wherePivot('created_at', '<=', CarbonImmutable::now()->endOfMonth())
->wherePivot('created_at', '>=', CarbonImmutable::now()->startOfMonth())
->count();
// get all the events of the current year
$yearlyEvents = $contact->lifeMetrics()
->where('life_metric_id', $lifeMetric->id)
->wherePivot('created_at', '<=', CarbonImmutable::now()->endOfYear())
->wherePivot('created_at', '>=', CarbonImmutable::now()->startOfYear())
->count();
return [
'weekly_events' => $weeklyEvents,
'monthly_events' => $monthlyEvents,
'yearly_events' => $yearlyEvents,
];
}
}