Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Entitlement; | use App\Entitlement; | ||||
use App\UserAlias; | use App\UserAlias; | ||||
use App\Sku; | |||||
use App\Traits\UserAliasesTrait; | use App\Traits\UserAliasesTrait; | ||||
use App\Traits\SettingsTrait; | use App\Traits\SettingsTrait; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Illuminate\Notifications\Notifiable; | use Illuminate\Notifications\Notifiable; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
use Illuminate\Foundation\Auth\User as Authenticatable; | use Illuminate\Foundation\Auth\User as Authenticatable; | ||||
use Iatstuti\Database\Support\NullableFields; | use Iatstuti\Database\Support\NullableFields; | ||||
use Tymon\JWTAuth\Contracts\JWTSubject; | use Tymon\JWTAuth\Contracts\JWTSubject; | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* Assign a Sku to a user. | * Assign a Sku to a user. | ||||
* | * | ||||
* @param \App\Sku $sku The sku to assign. | * @param \App\Sku $sku The sku to assign. | ||||
* @param int $count Count of entitlements to add | * @param int $count Count of entitlements to add | ||||
* | * | ||||
* @return \App\User Self | * @return \App\User Self | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public function assignSku($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 | // 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([ | ||||
'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 260 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 The SKU | |||||
* @param int $count The number of entitlements to remove | |||||
* | |||||
* @return User Self | |||||
*/ | |||||
public function removeSku(Sku $sku, int $count = 1): User | |||||
{ | |||||
$entitlements = $this->entitlements() | |||||
->where('sku_id', $sku->id) | |||||
machniak: I'm also thinking that maybe we should sort them also by created_by. First, to have the process… | |||||
->orderBy('cost', 'desc') | |||||
->orderBy('created_at') | |||||
->get(); | |||||
Not Done Inline ActionsWe fetched the list already, so I'd prefer to not count using sql, on every iteration. machniak: We fetched the list already, so I'd prefer to not count using sql, on every iteration. | |||||
$entitlements_count = count($entitlements); | |||||
foreach ($entitlements as $entitlement) { | |||||
if ($entitlements_count <= $sku->units_free) { | |||||
continue; | |||||
} | |||||
if ($count > 0) { | |||||
$entitlement->delete(); | |||||
$entitlements_count--; | |||||
$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 |
I'm also thinking that maybe we should sort them also by created_by. First, to have the process more defined. Second, to delete them in an order that will give us potenially more money (although, that's probably another rare corner-case).