Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 178 Lines • ▼ Show 20 Lines | public function assignSku($sku, int $count = 1): User | ||||
if ($sku->handler_class::entitleableClass() != User::class) { | if ($sku->handler_class::entitleableClass() != User::class) { | ||||
throw new \Exception("Cannot assign non-user SKU ({$sku->title}) to a user"); | 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' => $sku->units_free >= $exists ? $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 | ||||
]); | ]); | ||||
$exists++; | $exists++; | ||||
$count--; | $count--; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 256 Lines • ▼ Show 20 Lines | public function name(bool $fallback = false): string | ||||
if (empty($name) && $fallback) { | if (empty($name) && $fallback) { | ||||
return \config('app.name') . ' User'; | return \config('app.name') . ' User'; | ||||
} | } | ||||
return $name; | return $name; | ||||
} | } | ||||
/** | /** | ||||
* Remove a number of entitlements for the SKU. | |||||
* | |||||
* @param \App\Sku $sku | |||||
* @param int $count | |||||
* | |||||
* @return User | |||||
*/ | |||||
public function removeSku($sku, int $count = 1): User | |||||
{ | |||||
$entitlements = $this->entitlements()->where('sku_id', $sku->id) | |||||
->orderBy('cost', 'desc')->get(); | |||||
foreach ($entitlements as $entitlement) { | |||||
if ($this->entitlements()->where('sku_id', $sku->id)->count() <= $sku->units_free) { | |||||
continue; | |||||
} | |||||
if ($count > 0) { | |||||
$entitlement->delete(); | |||||
$count--; | |||||
} | |||||
} | |||||
return $this; | |||||
} | |||||
/** | |||||
* Any (additional) properties of this user. | * Any (additional) properties of this user. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function settings() | public function settings() | ||||
{ | { | ||||
return $this->hasMany('App\UserSetting', 'user_id'); | return $this->hasMany('App\UserSetting', 'user_id'); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 154 Lines • Show Last 20 Lines |