format usage estimates refactoring & test

This commit is contained in:
Čarodej
2021-12-16 13:44:36 +01:00
parent b4dfbd1623
commit 27753f30ad
3 changed files with 105 additions and 56 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Users\Actions;
use ByteUnits\Metric;
use Illuminate\Support\Collection;
class FormatUsageEstimatesAction
{
public function __invoke(string $currency, Collection $usage)
{
return $usage->map(function ($estimate) use ($currency) {
// Format usage
$usage = match ($estimate['feature']) {
'bandwidth' => Metric::megabytes($estimate['usage'])->format(),
'storage' => Metric::megabytes($estimate['usage'])->format(),
};
// Normalize units
$amount = $estimate['amount'] / 1000;
return [
'feature' => $estimate['feature'],
'amount' => $amount,
'cost' => format_currency($amount, $currency),
'usage' => $usage,
];
});
}
}