70 lines
1.7 KiB
PHP
70 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
/** Storage-tier pricing for mailboxes (config/email.php quota_tiers). */
|
|
class MailboxPricing
|
|
{
|
|
/** @return array<int,array{mb:int,price_minor:int,label:string}> */
|
|
public static function tiers(): array
|
|
{
|
|
return array_map(fn ($t) => [
|
|
'mb' => (int) $t['mb'],
|
|
'price_minor' => (int) $t['price_minor'],
|
|
'label' => self::label((int) $t['mb']),
|
|
], (array) config('email.quota_tiers', []));
|
|
}
|
|
|
|
public static function isValidQuota(int $mb): bool
|
|
{
|
|
foreach (self::tiers() as $t) {
|
|
if ($t['mb'] === $mb) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function priceMinorFor(int $mb): int
|
|
{
|
|
foreach (self::tiers() as $t) {
|
|
if ($t['mb'] === $mb) {
|
|
return $t['price_minor'];
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/** A tier is free when its monthly price is zero (the 1 GB plan). */
|
|
public static function isFree(int $mb): bool
|
|
{
|
|
return self::priceMinorFor($mb) === 0;
|
|
}
|
|
|
|
/** The smallest free tier's quota (the default for new mailboxes). */
|
|
public static function freeQuotaMb(): int
|
|
{
|
|
foreach (self::tiers() as $t) {
|
|
if ($t['price_minor'] === 0) {
|
|
return $t['mb'];
|
|
}
|
|
}
|
|
|
|
return self::tiers()[0]['mb'] ?? 1024;
|
|
}
|
|
|
|
public static function defaultQuotaMb(): int
|
|
{
|
|
$default = (int) config('email.default_quota_mb', 1024);
|
|
|
|
return self::isValidQuota($default) ? $default : self::freeQuotaMb();
|
|
}
|
|
|
|
public static function label(int $mb): string
|
|
{
|
|
return $mb % 1024 === 0 ? ($mb / 1024).' GB' : $mb.' MB';
|
|
}
|
|
}
|