Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 169 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public function assignSku(Sku $sku, int $count = 1): User | public function assignSku(Sku $sku, int $count = 1): User | ||||
{ | { | ||||
// TODO: I guess wallet could be parametrized in future | // TODO: I guess wallet could be parametrized in future | ||||
$wallet = $this->wallet(); | $wallet = $this->wallet(); | ||||
$exists = $this->entitlements()->where('sku_id', $sku->id)->count(); | $exists = $this->entitlements()->where('sku_id', $sku->id)->count(); | ||||
// TODO: Sanity check, this probably should be in preReq() on handlers | |||||
// or in EntitlementObserver | |||||
if ($sku->handler_class::entitleableClass() != User::class) { | |||||
throw new \Exception("Cannot assign non-user SKU ({$sku->title}) to a user"); | |||||
} | |||||
while ($count > 0) { | while ($count > 0) { | ||||
\App\Entitlement::create([ | \App\Entitlement::create([ | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'cost' => $exists >= $sku->units_free ? $sku->cost : 0, | 'cost' => $exists >= $sku->units_free ? $sku->cost : 0, | ||||
'entitleable_id' => $this->id, | 'entitleable_id' => $this->id, | ||||
'entitleable_type' => User::class | 'entitleable_type' => User::class | ||||
]); | ]); | ||||
▲ Show 20 Lines • Show All 529 Lines • Show Last 20 Lines |