From e41f3e8a6738670152d00eb191c11f506efc0cbf Mon Sep 17 00:00:00 2001 From: Abdel LAMRANI Date: Fri, 17 Jul 2026 16:46:22 +0200 Subject: [PATCH] fix :bug: error sentry on version 7.3 --- app/Http/Controllers/Bo/StatsController.php | 81 ++++++++++++++------- app/Http/Requests/Bo/StatsRequest.php | 21 ++++-- 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/Bo/StatsController.php b/app/Http/Controllers/Bo/StatsController.php index aacae03..ac86b3b 100644 --- a/app/Http/Controllers/Bo/StatsController.php +++ b/app/Http/Controllers/Bo/StatsController.php @@ -72,29 +72,43 @@ class StatsController extends Controller * - total of specific url from a brand * - total of all urls for each brand */ - $datasets = match (true) { - !$requestValidated['brand'] => $this->buildDatasets( + if (!$requestValidated['brand']) { + $datasets = $this->buildDatasets( Brand::all(), $periodInterval, $requestValidated['period'], - fn($brand, $date, $period) => $this->getCallsSum($brand, $date, $period), - fn($brand) => $brand->name - ), - !$requestValidated['url'] => $this->buildDatasets( + function ($brand, $date, $period) { + return $this->getCallsSum($brand, $date, $period); + }, + function ($brand) { + return $brand->name; + } + ); + } elseif (!$requestValidated['url']) { + $datasets = $this->buildDatasets( Brand::find($requestValidated['brand'])->urls, $periodInterval, $requestValidated['period'], - fn($url, $date, $period) => $this->getCallsSum($url, $date, $period), - fn($url) => $url->value - ), - default => $this->buildDatasets( + function ($url, $date, $period) { + return $this->getCallsSum($url, $date, $period); + }, + function ($url) { + return $url->value; + } + ); + } else { + $datasets = $this->buildDatasets( [Url::find($requestValidated['url'])], $periodInterval, $requestValidated['period'], - fn($url, $date, $period) => $this->getCallsSum($url, $date, $period), - fn($url) => $url->value - ), - }; //end match + function ($url, $date, $period) { + return $this->getCallsSum($url, $date, $period); + }, + function ($url) { + return $url->value; + } + ); + } // Get the type of graph when there are several data or only one. $chartType = count(Arr::first($datasets)['data'] ?? []) > 1 ? 'line' : 'bar'; @@ -157,15 +171,21 @@ class StatsController extends Controller return UrlStats::query() ->when( $period === PeriodEnum::day()->value, - fn($query) => $query->whereDate('date', $date) + function ($query) use ($date) { + return $query->whereDate('date', $date); + } ) ->when( $period === PeriodEnum::week()->value, - fn($query) => $query->whereBetween('date', [$date->copy()->startOfWeek(), $date->copy()->endOfWeek()]) + function ($query) use ($date) { + return $query->whereBetween('date', [$date->copy()->startOfWeek(), $date->copy()->endOfWeek()]); + } ) ->when( $period === PeriodEnum::month()->value, - fn($query) => $query->whereBetween('date', [$date->copy()->startOfMonth(), $date->copy()->endOfMonth()]) + function ($query) use ($date) { + return $query->whereBetween('date', [$date->copy()->startOfMonth(), $date->copy()->endOfMonth()]); + } ) ->whereIn('url_id', $urlIds) ->sum('calls'); @@ -240,17 +260,22 @@ class StatsController extends Controller private function getChartLabels(Carbon $date, string $period): array { // Match given period with enum names. - return match ($period) { - $this->getEnumName(PeriodEnum::week()) => array_unique([sprintf( - 'Du %s au %s', - $date->startOfWeek()->format('Y-m-d'), - $date->endOfWeek()->format('Y-m-d') - )]), - $this->getEnumName(PeriodEnum::month()) => array_unique([ - Str::ucfirst($date->monthName) - ]), - default => array_unique([$date->format('d-m-Y')]), - }; + switch ($period) { + case $this->getEnumName(PeriodEnum::week()): + return array_unique([sprintf( + 'Du %s au %s', + $date->startOfWeek()->format('Y-m-d'), + $date->endOfWeek()->format('Y-m-d') + )]); + + case $this->getEnumName(PeriodEnum::month()): + return array_unique([ + Str::ucfirst($date->monthName) + ]); + + default: + return array_unique([$date->format('d-m-Y')]); + } } /** diff --git a/app/Http/Requests/Bo/StatsRequest.php b/app/Http/Requests/Bo/StatsRequest.php index 206d071..65a9a86 100644 --- a/app/Http/Requests/Bo/StatsRequest.php +++ b/app/Http/Requests/Bo/StatsRequest.php @@ -34,11 +34,22 @@ class StatsRequest extends FormRequest $adjustedEndDate = $endDate->greaterThan($now) ? $now : $endDate; // Get start & end dates by period. - [$adjustedStart, $adjustedEnd] = match ($this->input('period')) { - 'week' => [$startDate->copy()->startOfWeek(), $endDate->copy()->endOfWeek()], - 'month' => [$startDate->copy()->startOfMonth(), $endDate->copy()->endOfMonth()], - default => [$startDate, $adjustedEndDate], - }; + switch ($this->input('period')) { + case 'week': + $adjustedStart = $startDate->copy()->startOfWeek(); + $adjustedEnd = $endDate->copy()->endOfWeek(); + break; + + case 'month': + $adjustedStart = $startDate->copy()->startOfMonth(); + $adjustedEnd = $endDate->copy()->endOfMonth(); + break; + + default: + $adjustedStart = $startDate; + $adjustedEnd = $adjustedEndDate; + break; + } $this->merge([ 'url' => Url::find($urlId) ? $urlId : null,