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 127 Lines • ▼ Show 20 Lines | public function domains() | ||||
\Log::info("Found domain {$this->email}: {$domain->namespace} (charged)"); | \Log::info("Found domain {$this->email}: {$domain->namespace} (charged)"); | ||||
$domains[] = $domain; | $domains[] = $domain; | ||||
} | } | ||||
} | } | ||||
return $domains; | return $domains; | ||||
} | } | ||||
/** | |||||
* The user entitlement. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\MorphOne | |||||
*/ | |||||
public function entitlement() | public function entitlement() | ||||
{ | { | ||||
return $this->morphOne('App\Entitlement', 'entitleable'); | return $this->morphOne('App\Entitlement', 'entitleable'); | ||||
} | } | ||||
/** | /** | ||||
* Entitlements for this user. | * Entitlements for this user. | ||||
* | * | ||||
* Note that these are entitlements that apply to the user account, and not entitlements that | * Note that these are entitlements that apply to the user account, and not entitlements that | ||||
* this user owns. | * this user owns. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function entitlements() | public function entitlements() | ||||
{ | { | ||||
return $this->hasMany('App\Entitlement', 'entitleable_id', 'id'); | return $this->hasMany('App\Entitlement', 'entitleable_id', 'id') | ||||
->where('entitleable_type', User::class); | |||||
} | } | ||||
public function addEntitlement($entitlement) | public function addEntitlement($entitlement) | ||||
{ | { | ||||
if (!$this->entitlements->contains($entitlement)) { | if (!$this->entitlements->contains($entitlement)) { | ||||
return $this->entitlements()->save($entitlement); | return $this->entitlements()->save($entitlement); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 384 Lines • Show Last 20 Lines |