Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | public function assignPackage($package, $user = null) | ||||
} | } | ||||
$wallet_id = $this->wallets()->first()->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, | |||||
'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 | ||||
] | ] | ||||
); | ); | ||||
} | } | ||||
Show All 33 Lines | class User extends Authenticatable implements JWTSubject | ||||
* | * | ||||
* @return \App\User Self | * @return \App\User Self | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public function assignSku($sku, int $count = 1): User | public function assignSku($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(); | ||||
$owner = $wallet->owner; | |||||
$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 | // TODO: Sanity check, this probably should be in preReq() on handlers | ||||
// or in EntitlementObserver | // or in EntitlementObserver | ||||
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([ | ||||
'owner_id' => $owner->id, | |||||
'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' => $sku->units_free >= $exists ? $sku->cost : 0, | ||||
'entitleable_id' => $this->id, | 'entitleable_id' => $this->id, | ||||
'entitleable_type' => User::class | 'entitleable_type' => User::class | ||||
]); | ]); | ||||
$exists++; | $exists++; | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | public function domains() | ||||
)->get(); | )->get(); | ||||
$domains = []; | $domains = []; | ||||
foreach ($dbdomains as $dbdomain) { | foreach ($dbdomains as $dbdomain) { | ||||
$domains[] = $dbdomain; | $domains[] = $dbdomain; | ||||
} | } | ||||
$entitlements = Entitlement::where('owner_id', $this->id)->get(); | foreach ($this->wallets as $wallet) { | ||||
$entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | |||||
foreach ($entitlements as $entitlement) { | foreach ($entitlements as $entitlement) { | ||||
if ($entitlement->entitleable instanceof Domain) { | |||||
$domain = $entitlement->entitleable; | $domain = $entitlement->entitleable; | ||||
\Log::info("Found domain for {$this->email}: {$domain->namespace} (owned)"); | \Log::info("Found domain for {$this->email}: {$domain->namespace} (owned)"); | ||||
$domains[] = $domain; | $domains[] = $domain; | ||||
} | } | ||||
} | } | ||||
foreach ($this->accounts as $wallet) { | foreach ($this->accounts as $wallet) { | ||||
foreach ($wallet->entitlements as $entitlement) { | $entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | ||||
if ($entitlement->entitleable instanceof Domain) { | foreach ($entitlements as $entitlement) { | ||||
$domain = $entitlement->entitleable; | $domain = $entitlement->entitleable; | ||||
\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; | ||||
} | } | ||||
public function entitlement() | public function entitlement() | ||||
{ | { | ||||
return $this->morphOne('App\Entitlement', 'entitleable'); | return $this->morphOne('App\Entitlement', 'entitleable'); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 256 Lines • Show Last 20 Lines |