Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/SkusController.php
Show All 20 Lines | public function index() | ||||
// Note: Order by title for consistent ordering in tests | // Note: Order by title for consistent ordering in tests | ||||
$response = Sku::withSubjectTenantContext()->where('active', true)->orderBy('title') | $response = Sku::withSubjectTenantContext()->where('active', true)->orderBy('title') | ||||
->get() | ->get() | ||||
->transform(function ($sku) { | ->transform(function ($sku) { | ||||
return $this->skuElement($sku); | return $this->skuElement($sku); | ||||
}) | }) | ||||
->filter(function ($sku) use ($type) { | ->filter(function ($sku) use ($type) { | ||||
return !$type || $sku['type'] === $type; | return $sku && (!$type || $sku['type'] === $type); | ||||
}) | }) | ||||
->sortByDesc('prio') | ->sortByDesc('prio') | ||||
->values(); | ->values(); | ||||
if ($type) { | if ($type) { | ||||
$wallet = $this->guard()->user()->wallet(); | $wallet = $this->guard()->user()->wallet(); | ||||
// Figure out the cost for a new object of the specified type | // Figure out the cost for a new object of the specified type | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | public static function updateEntitlements($object, $rSkus, $wallet = null): void | ||||
$eSkus[$e->sku_id] = $e->total; | $eSkus[$e->sku_id] = $e->total; | ||||
} | } | ||||
); | ); | ||||
foreach ($skus as $skuID => $sku) { | foreach ($skus as $skuID => $sku) { | ||||
$e = array_key_exists($skuID, $eSkus) ? $eSkus[$skuID] : 0; | $e = array_key_exists($skuID, $eSkus) ? $eSkus[$skuID] : 0; | ||||
$r = array_key_exists($skuID, $rSkus) ? $rSkus[$skuID] : 0; | $r = array_key_exists($skuID, $rSkus) ? $rSkus[$skuID] : 0; | ||||
if (!is_a($object, $sku->handler_class::entitleableClass())) { | if (!class_exists($sku->handler_class) || !is_a($object, $sku->handler_class::entitleableClass())) { | ||||
continue; | continue; | ||||
} | } | ||||
if ($sku->handler_class == \App\Handlers\Mailbox::class) { | if ($sku->handler_class == \App\Handlers\Mailbox::class) { | ||||
if ($r != 1) { | if ($r != 1) { | ||||
throw new \Exception("Invalid quantity of mailboxes"); | throw new \Exception("Invalid quantity of mailboxes"); | ||||
} | } | ||||
} | } | ||||
Show All 14 Lines | class SkusController extends ResourceController | ||||
* | * | ||||
* @param \App\Sku $sku SKU object | * @param \App\Sku $sku SKU object | ||||
* | * | ||||
* @return array|null Metadata | * @return array|null Metadata | ||||
*/ | */ | ||||
protected static function skuElement($sku): ?array | protected static function skuElement($sku): ?array | ||||
{ | { | ||||
if (!class_exists($sku->handler_class)) { | if (!class_exists($sku->handler_class)) { | ||||
\Log::warning("Missing handler {$sku->handler_class}"); | |||||
return null; | return null; | ||||
} | } | ||||
$data = array_merge($sku->toArray(), $sku->handler_class::metadata($sku)); | $data = array_merge($sku->toArray(), $sku->handler_class::metadata($sku)); | ||||
// ignore incomplete handlers | // ignore incomplete handlers | ||||
if (empty($data['type'])) { | if (empty($data['type'])) { | ||||
\Log::warning("Incomplete handler {$sku->handler_class}"); | |||||
return null; | return null; | ||||
} | } | ||||
// Use localized value, toArray() does not get them right | // Use localized value, toArray() does not get them right | ||||
$data['name'] = $sku->name; | $data['name'] = $sku->name; | ||||
$data['description'] = $sku->description; | $data['description'] = $sku->description; | ||||
unset($data['handler_class'], $data['created_at'], $data['updated_at'], $data['fee'], $data['tenant_id']); | unset($data['handler_class'], $data['created_at'], $data['updated_at'], $data['fee'], $data['tenant_id']); | ||||
return $data; | return $data; | ||||
} | } | ||||
} | } |