Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/EntitleableTrait.php
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | public function assignToWallet(Wallet $wallet) | ||||
if ($this->entitlements()->count()) { | if ($this->entitlements()->count()) { | ||||
throw new \Exception("Object already assigned to a wallet"); | throw new \Exception("Object already assigned to a wallet"); | ||||
} | } | ||||
// Find the SKU title, e.g. \App\SharedFolder -> shared-folder | // Find the SKU title, e.g. \App\SharedFolder -> shared-folder | ||||
// Note: it does not work with User/Domain model (yet) | // Note: it does not work with User/Domain model (yet) | ||||
$title = Str::kebab(\class_basename(self::class)); | $title = Str::kebab(\class_basename(self::class)); | ||||
$sku = Sku::withObjectTenantContext($this)->where('title', $title)->first(); | $sku = $this->skuByTitle($title); | ||||
$exists = $wallet->entitlements()->where('sku_id', $sku->id)->count(); | $exists = $wallet->entitlements()->where('sku_id', $sku->id)->count(); | ||||
Entitlement::create([ | 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, | ||||
'fee' => $exists >= $sku->units_free ? $sku->fee : 0, | 'fee' => $exists >= $sku->units_free ? $sku->fee : 0, | ||||
'entitleable_id' => $this->id, | 'entitleable_id' => $this->id, | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | trait EntitleableTrait | ||||
* Check if an entitlement for the specified SKU exists. | * Check if an entitlement for the specified SKU exists. | ||||
* | * | ||||
* @param string $title The SKU title | * @param string $title The SKU title | ||||
* | * | ||||
* @return bool True if specified SKU entitlement exists | * @return bool True if specified SKU entitlement exists | ||||
*/ | */ | ||||
public function hasSku(string $title): bool | public function hasSku(string $title): bool | ||||
{ | { | ||||
$sku = Sku::withObjectTenantContext($this)->where('title', $title)->first(); | $sku = $this->skuByTitle($title); | ||||
if (!$sku) { | if (!$sku) { | ||||
return false; | return false; | ||||
} | } | ||||
return $this->entitlements()->where('sku_id', $sku->id)->count() > 0; | return $this->entitlements()->where('sku_id', $sku->id)->count() > 0; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | public function restoreEntitlements(): void | ||||
// Note: We're assuming that cost of entitlements was correct | // Note: We're assuming that cost of entitlements was correct | ||||
// on deletion, so we don't have to re-calculate it again. | // on deletion, so we don't have to re-calculate it again. | ||||
// TODO: We should probably re-calculate the cost | // TODO: We should probably re-calculate the cost | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Find the SKU object by title. Use current object's tenant context. | |||||
* | |||||
* @param string $title SKU title. | |||||
* | |||||
* @return ?\App\Sku A SKU object | |||||
*/ | |||||
protected function skuByTitle(string $title): ?Sku | |||||
{ | |||||
return Sku::withObjectTenantContext($this)->where('title', $title)->first(); | |||||
} | |||||
/** | |||||
* Returns entitleable object title (e.g. email or domain name). | |||||
* | |||||
* @return string|null An object title/name | |||||
*/ | |||||
public function toString(): ?string | |||||
{ | |||||
// This method should be overloaded by the model class | |||||
// if the object has not email attribute | |||||
return $this->email; | |||||
} | |||||
/** | |||||
* Returns the wallet by which the object is controlled | * Returns the wallet by which the object is controlled | ||||
* | * | ||||
* @return ?\App\Wallet A wallet object | * @return ?\App\Wallet A wallet object | ||||
*/ | */ | ||||
public function wallet(): ?Wallet | public function wallet(): ?Wallet | ||||
{ | { | ||||
$entitlement = $this->entitlements()->withTrashed()->orderBy('created_at', 'desc')->first(); | $entitlement = $this->entitlements()->withTrashed()->orderBy('created_at', 'desc')->first(); | ||||
Show All 33 Lines |