Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* @return \App\User | * @return \App\User | ||||
*/ | */ | ||||
public function assignPackage($package, $user = null) | public function assignPackage($package, $user = null) | ||||
{ | { | ||||
if (!$user) { | if (!$user) { | ||||
$user = $this; | $user = $this; | ||||
} | } | ||||
$wallet_id = $this->wallets()->get()[0]->id; | $wallet_id = $this->wallets()->first()->id; | ||||
foreach ($package->skus as $sku) { | foreach ($package->skus as $sku) { | ||||
for ($i = $sku->pivot->qty; $i > 0; $i--) { | for ($i = $sku->pivot->qty; $i > 0; $i--) { | ||||
\App\Entitlement::create( | \App\Entitlement::create( | ||||
[ | [ | ||||
'owner_id' => $this->id, | 'owner_id' => $this->id, | ||||
'wallet_id' => $wallet_id, | 'wallet_id' => $wallet_id, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'cost' => $sku->pivot->cost(), | 'cost' => $sku->pivot->cost(), | ||||
'entitleable_id' => $user->id, | 'entitleable_id' => $user->id, | ||||
'entitleable_type' => User::class | 'entitleable_type' => User::class | ||||
] | ] | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
return $user; | return $user; | ||||
} | } | ||||
public function assignPlan($plan, $domain = null) | /** | ||||
* Assign a package plan to a user. | |||||
* | |||||
* @param \App\Plan $plan The plan to assign | |||||
* @param \App\Domain $domain Optional domain object | |||||
* | |||||
* @return \App\User Self | |||||
*/ | |||||
public function assignPlan($plan, $domain = null): User | |||||
{ | { | ||||
$this->setSetting('plan_id', $plan->id); | $this->setSetting('plan_id', $plan->id); | ||||
foreach ($plan->packages as $package) { | foreach ($plan->packages as $package) { | ||||
if ($package->isDomain()) { | if ($package->isDomain()) { | ||||
$domain->assignPackage($package, $this); | $domain->assignPackage($package, $this); | ||||
} else { | } else { | ||||
$this->assignPackage($package); | $this->assignPackage($package); | ||||
} | } | ||||
} | } | ||||
return $this; | |||||
} | |||||
/** | |||||
* Assign a Sku to a user. | |||||
* | |||||
* @param \App\Sku $sku The sku to assign. | |||||
* @param int $count Count of entitlements to add | |||||
* | |||||
* @return \App\User Self | |||||
* @throws \Exception | |||||
*/ | |||||
public function assignSku($sku, int $count = 1): User | |||||
{ | |||||
// TODO: I guess wallet could be parametrized in future | |||||
$wallet = $this->wallet(); | |||||
$owner = $wallet->owner; | |||||
$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) { | |||||
\App\Entitlement::create([ | |||||
'owner_id' => $owner->id, | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => $sku->units_free >= $exists ? $sku->cost : 0, | |||||
'entitleable_id' => $this->id, | |||||
'entitleable_type' => User::class | |||||
]); | |||||
$exists++; | |||||
$count--; | |||||
} | |||||
return $this; | |||||
} | } | ||||
/** | /** | ||||
* Check if current user can delete another object. | * Check if current user can delete another object. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object A user|domain object | * @param \App\User|\App\Domain $object A user|domain object | ||||
* | * | ||||
* @return bool True if he can, False otherwise | * @return bool True if he can, False otherwise | ||||
▲ Show 20 Lines • Show All 361 Lines • Show Last 20 Lines |