Files
Zonneplan/app/Features/Dashboard/Services/DashboardService.php
T

46 lines
1.4 KiB
PHP

<?php
namespace App\Features\Dashboard\Services;
use App\Contracts\PriceRepository;
use App\Features\Dashboard\Data\DashboardData;
use Carbon\Carbon;
class DashboardService
{
public function __construct(
public PriceRepository $repository
) {}
public function getDashboardData(): DashboardData
{
$electricity = $this->repository->retrieveElectricity();
$gas = $this->repository->retrieveGas();
$now = Carbon::now();
$eNow = $electricity->first(
fn ($item) => $item->startDate instanceof Carbon
&& $item->startDate->gte($now->copy()->subHour())
&& $item->startDate->lte($now)
);
$eCheapest = $electricity->where('totalPriceTaxIncluded', $electricity->min('totalPriceTaxIncluded'))->first();
$eDurable = $electricity->where('sustainabilityScore', $electricity->max('sustainabilityScore'))->first();
$ePriciest = $electricity->where('totalPriceTaxIncluded', $electricity->max('totalPriceTaxIncluded'))->first();
$gNow = $gas->first(
fn ($item) => $item->startDate instanceof Carbon
&& $item->startDate->gte($now->copy()->startOfDay())
&& $item->startDate->lte($now)
);
return new DashboardData(
$eNow,
$eCheapest,
$eDurable,
$ePriciest,
$gNow,
);
}
}