refactor(dashboard): move price clients to app namespace and simplify data
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Dashboard\Clients;
|
||||
|
||||
use App\Features\Dashboard\Contracts\PriceClient;
|
||||
use Exception;
|
||||
use Illuminate\Http\Client\Factory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class ZonneplanPriceClient implements PriceClient
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Factory $http,
|
||||
) {}
|
||||
|
||||
public function getElectricityPrices(): Collection
|
||||
{
|
||||
return collect($this->get('/energy-prices/electricity/upcoming'));
|
||||
}
|
||||
|
||||
public function getGasPrices(): Collection
|
||||
{
|
||||
return collect($this->get('/energy-prices/gas/upcoming'));
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
private function get(string $path): array
|
||||
{
|
||||
try {
|
||||
$response = $this->http
|
||||
->baseUrl(config('api.baseurl'))
|
||||
->withQueryParameters([
|
||||
'secret' => config('api.key'),
|
||||
])
|
||||
->timeout(10)
|
||||
->retry(2, 100)
|
||||
->get($path);
|
||||
|
||||
$response->throw();
|
||||
|
||||
Log::info('Zonneplan API opgehaald', [
|
||||
'path' => $path,
|
||||
'status' => $response->status(),
|
||||
]);
|
||||
|
||||
return $response->json('data', []);
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Zonneplan API aanroep mislukt', [
|
||||
'path' => $path,
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
throw new Exception(
|
||||
"Kon energieprijzen niet ophalen voor {$path}",
|
||||
previous: $e,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Dashboard\Contracts;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface PriceClient
|
||||
{
|
||||
public function getElectricityPrices(): Collection;
|
||||
|
||||
public function getGasPrices(): Collection;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Dashboard\Contracts;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface PriceRepository
|
||||
{
|
||||
public function retrieveElectricity(): Collection;
|
||||
|
||||
public function retrieveGas(): Collection;
|
||||
}
|
||||
@@ -2,23 +2,16 @@
|
||||
|
||||
namespace App\Features\Dashboard\Data;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\LaravelData\Data;
|
||||
|
||||
class DashboardData extends Data
|
||||
{
|
||||
/**
|
||||
* @property Collection<ElectricityData> $electricityHistory
|
||||
* @property Collection<GasData> $gasHistory
|
||||
*/
|
||||
public function __construct(
|
||||
/** @var array<ElectricityData> */
|
||||
public Collection $electricityHistory,
|
||||
public ElectricityData $electricity,
|
||||
public ?ElectricityData $electricityCheapest,
|
||||
public ?ElectricityData $electricityDurable,
|
||||
public ?ElectricityData $electricityPriciest,
|
||||
/** @var array<ElectricityData> */
|
||||
public Collection $gasHistory,
|
||||
public GasData $gas,
|
||||
public ?GasData $gasCheapest,
|
||||
public ?GasData $gasPriciest,
|
||||
) {}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Dashboard\Repositories;
|
||||
|
||||
use App\Features\Dashboard\Contracts\PriceClient;
|
||||
use App\Features\Dashboard\Contracts\PriceRepository as ContractsPriceRepository;
|
||||
use App\Features\Dashboard\Data\ElectricityData;
|
||||
use App\Features\Dashboard\Data\GasData;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Override;
|
||||
|
||||
class ZonneplanPriceRepository implements ContractsPriceRepository
|
||||
{
|
||||
public function __construct(
|
||||
public PriceClient $client
|
||||
) {}
|
||||
|
||||
#[Override]
|
||||
public function retrieveElectricity(): Collection
|
||||
{
|
||||
$data = Cache::remember('electricity', 3600, fn () => $this->client->getElectricityPrices()->toArray());
|
||||
|
||||
return collect(ElectricityData::collect($data));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function retrieveGas(): Collection
|
||||
{
|
||||
$data = Cache::remember('gas', 3600, fn () => $this->client->getGasPrices()->toArray());
|
||||
|
||||
return collect(GasData::collect($data));
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
namespace App\Features\Dashboard\Services;
|
||||
|
||||
use App\Features\Dashboard\Contracts\PriceRepository;
|
||||
use App\Contracts\PriceRepository;
|
||||
use App\Features\Dashboard\Data\DashboardData;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class DashboardService
|
||||
{
|
||||
@@ -13,21 +14,26 @@ class DashboardService
|
||||
|
||||
public function getDashboardData(): DashboardData
|
||||
{
|
||||
$now = Carbon::now();
|
||||
|
||||
$electricity = $this->repository->retrieveElectricity();
|
||||
$eCheapest = $electricity->min('total_price_tax_included');
|
||||
$eDurable = $electricity->min('sustainability_score');
|
||||
$ePriciest = $electricity->max('total_price_tax_included');
|
||||
|
||||
$eNow = $electricity->whereBetween('startDate', [$now->copy()->subHour(), $now])->first();
|
||||
$eCheapest = $electricity->where('totalPriceTaxIncluded', $electricity->min('totalPriceTaxIncluded'))->first();
|
||||
$eDurable = $electricity->where('sustainabilityScore', $electricity->min('sustainabilityScore'))->first();
|
||||
$ePriciest = $electricity->where('totalPriceTaxIncluded', $electricity->max('totalPriceTaxIncluded'))->first();
|
||||
|
||||
$gas = $this->repository->retrieveGas();
|
||||
$gCheapest = $gas->min('total_price_tax_included');
|
||||
$gPriciest = $gas->min('total_price_tax_included');
|
||||
$gNow = $gas->whereBetween('startDate', [$now->copy()->setHour(0), $now])->first();
|
||||
$gCheapest = $gas->where('totalPriceTaxIncluded', $gas->min('totalPriceTaxIncluded'))->first();
|
||||
$gPriciest = $gas->where('totalPriceTaxIncluded', $gas->min('totalPriceTaxIncluded'))->first();
|
||||
|
||||
return new DashboardData(
|
||||
$electricity,
|
||||
$eNow,
|
||||
$eCheapest,
|
||||
$eDurable,
|
||||
$ePriciest,
|
||||
$gas,
|
||||
$gNow,
|
||||
$gCheapest,
|
||||
$gPriciest
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user