Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/SkusController.php
Show All 27 Lines | class SkusController extends Controller | ||||
*/ | */ | ||||
public function destroy($id) | public function destroy($id) | ||||
{ | { | ||||
// TODO | // TODO | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
/** | /** | ||||
* Get a list of SKUs available to the domain. | |||||
* | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function domainSkus($id) | |||||
{ | |||||
$domain = \App\Domain::find($id); | |||||
if (!$this->checkTenant($domain)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
if (!$this->guard()->user()->canRead($domain)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
return $this->objectSkus($domain); | |||||
} | |||||
/** | |||||
* Show the form for editing the specified sku. | * Show the form for editing the specified sku. | ||||
* | * | ||||
* @param int $id SKU identifier | * @param int $id SKU identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function edit($id) | public function edit($id) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | public function userSkus($id) | ||||
if (!$this->checkTenant($user)) { | if (!$this->checkTenant($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
if (!$this->guard()->user()->canRead($user)) { | if (!$this->guard()->user()->canRead($user)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
$type = request()->input('type'); | return $this->objectSkus($user); | ||||
} | |||||
/** | |||||
* Return SKUs available to the specified user/domain. | |||||
* | |||||
* @param object $object User or Domain object | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
protected static function objectSkus($object) | |||||
{ | |||||
$type = $object instanceof \App\Domain ? 'domain' : 'user'; | |||||
$response = []; | $response = []; | ||||
// Note: Order by title for consistent ordering in tests | // Note: Order by title for consistent ordering in tests | ||||
$skus = Sku::withObjectTenantContext($user)->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 (!$sku->handler_class::isAvailable($sku, $user)) { | if (!$sku->handler_class::isAvailable($sku, $object)) { | ||||
continue; | continue; | ||||
} | } | ||||
if ($data = $this->skuElement($sku)) { | if ($data = self::skuElement($sku)) { | ||||
if ($type && $type != $data['type']) { | if ($type != $data['type']) { | ||||
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']); | ||||
}); | }); | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* 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 | ||||
* | * | ||||
* @param \App\Sku $sku SKU object | * @param \App\Sku $sku SKU object | ||||
* | * | ||||
* @return array|null Metadata | * @return array|null Metadata | ||||
*/ | */ | ||||
protected function skuElement($sku): ?array | protected static function skuElement($sku): ?array | ||||
{ | { | ||||
if (!class_exists($sku->handler_class)) { | if (!class_exists($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 | ||||
Show All 13 Lines |