Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/SkusController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4; | namespace App\Http\Controllers\API\V4; | ||||
use App\Http\Controllers\ResourceController; | use App\Http\Controllers\ResourceController; | ||||
use App\Sku; | use App\Sku; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\Auth; | |||||
use Illuminate\Support\Str; | |||||
class SkusController extends ResourceController | class SkusController extends ResourceController | ||||
{ | { | ||||
/** | /** | ||||
* Get a list of active SKUs. | * Get a list of active SKUs. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
Show All 30 Lines | public function index() | ||||
return $sku; | return $sku; | ||||
}); | }); | ||||
} | } | ||||
return response()->json($response->all()); | return response()->json($response->all()); | ||||
} | } | ||||
/** | /** | ||||
* Return SKUs available to the specified user/domain. | * Return SKUs available to the specified entitleable object. | ||||
* | * | ||||
* @param object $object User or Domain object | * @param object $object Entitleable object | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public static function objectSkus($object) | public static function objectSkus($object) | ||||
{ | { | ||||
$type = \lcfirst(\class_basename($object::class)); | |||||
$response = []; | $response = []; | ||||
// Note: Order by title for consistent ordering in tests | // Note: Order by title for consistent ordering in tests | ||||
$skus = Sku::withObjectTenantContext($object)->orderBy('title')->get(); | $skus = Sku::withObjectTenantContext($object)->orderBy('title')->get(); | ||||
foreach ($skus as $sku) { | foreach ($skus as $sku) { | ||||
if (!class_exists($sku->handler_class)) { | if (!class_exists($sku->handler_class)) { | ||||
continue; | continue; | ||||
} | } | ||||
if ($object::class != $sku->handler_class::entitleableClass()) { | |||||
continue; | |||||
} | |||||
if (!$sku->handler_class::isAvailable($sku, $object)) { | if (!$sku->handler_class::isAvailable($sku, $object)) { | ||||
continue; | continue; | ||||
} | } | ||||
if ($data = self::skuElement($sku)) { | if ($data = self::skuElement($sku)) { | ||||
if ($type != $data['type']) { | if (!empty($data['controllerOnly'])) { | ||||
$user = Auth::guard()->user(); | |||||
if (!$user->wallet()->isController($user)) { | |||||
continue; | continue; | ||||
} | } | ||||
} | |||||
$response[] = $data; | $response[] = $data; | ||||
} | } | ||||
} | } | ||||
usort($response, function ($a, $b) { | usort($response, function ($a, $b) { | ||||
return ($b['prio'] <=> $a['prio']); | return ($b['prio'] <=> $a['prio']); | ||||
}); | }); | ||||
Show All 19 Lines | public static function objectEntitlements($object, &$response = []): void | ||||
$response['wallet']['discount'] = $wallet->discount->discount; | $response['wallet']['discount'] = $wallet->discount->discount; | ||||
$response['wallet']['discount_description'] = $wallet->discount->description; | $response['wallet']['discount_description'] = $wallet->discount->description; | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Update object entitlements. | * Update object entitlements. | ||||
* | * | ||||
* @param object $object The object for update | * @param object $object The object for update | ||||
* @param array $rSkus List of SKU IDs requested for the user in the form [id=>qty] | * @param array $rSkus List of SKU IDs requested for the object in the form [id=>qty] | ||||
* @param ?\App\Wallet $wallet The target wallet | |||||
*/ | */ | ||||
public static function updateEntitlements($object, $rSkus): void | public static function updateEntitlements($object, $rSkus, $wallet = null): void | ||||
{ | { | ||||
if (!is_array($rSkus)) { | if (!is_array($rSkus)) { | ||||
return; | return; | ||||
} | } | ||||
// list of skus, [id=>obj] | // list of skus, [id=>obj] | ||||
$skus = Sku::withEnvTenantContext()->get()->mapWithKeys( | $skus = Sku::withEnvTenantContext()->get()->mapWithKeys( | ||||
function ($sku) { | function ($sku) { | ||||
Show All 10 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())) { | |||||
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"); | ||||
} | } | ||||
} | } | ||||
if ($e > $r) { | if ($e > $r) { | ||||
// remove those entitled more than existing | // remove those entitled more than existing | ||||
$object->removeSku($sku, ($e - $r)); | $object->removeSku($sku, ($e - $r)); | ||||
} elseif ($e < $r) { | } elseif ($e < $r) { | ||||
// add those requested more than entitled | // add those requested more than entitled | ||||
$object->assignSku($sku, ($r - $e)); | $object->assignSku($sku, ($r - $e), $wallet); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Convert SKU information to metadata used by UI to | * Convert SKU information to metadata used by UI to | ||||
* display the form control | * display the form control | ||||
* | * | ||||
Show All 26 Lines |