Changeset View
Changeset View
Standalone View
Standalone View
src/app/Resource.php
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Line(s) | |||||
'status', | 'status', | ||||
]; | ]; | ||||
/** @var ?string Domain name for a resource to be created */ | /** @var ?string Domain name for a resource to be created */ | ||||
public $domain; | public $domain; | ||||
/** | /** | ||||
* Assign the resource to a wallet. | |||||
* | |||||
* @param \App\Wallet $wallet The wallet | |||||
* | |||||
* @return \App\Resource Self | |||||
* @throws \Exception | |||||
*/ | |||||
public function assignToWallet(Wallet $wallet): Resource | |||||
{ | |||||
if (empty($this->id)) { | |||||
throw new \Exception("Resource not yet exists"); | |||||
} | |||||
if ($this->entitlements()->count()) { | |||||
throw new \Exception("Resource already assigned to a wallet"); | |||||
} | |||||
$sku = \App\Sku::withObjectTenantContext($this)->where('title', 'resource')->first(); | |||||
$exists = $wallet->entitlements()->where('sku_id', $sku->id)->count(); | |||||
\App\Entitlement::create([ | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => $exists >= $sku->units_free ? $sku->cost : 0, | |||||
'fee' => $exists >= $sku->units_free ? $sku->fee : 0, | |||||
'entitleable_id' => $this->id, | |||||
'entitleable_type' => Resource::class | |||||
]); | |||||
return $this; | |||||
} | |||||
/** | |||||
* Returns the resource domain. | * Returns the resource domain. | ||||
* | * | ||||
* @return ?\App\Domain The domain to which the resource belongs to, NULL if it does not exist | * @return ?\App\Domain The domain to which the resource belongs to, NULL if it does not exist | ||||
*/ | */ | ||||
public function domain(): ?Domain | public function domain(): ?Domain | ||||
{ | { | ||||
if (isset($this->domain)) { | if (isset($this->domain)) { | ||||
$domainName = $this->domain; | $domainName = $this->domain; | ||||
▲ Show 20 Lines • Show All 113 Lines • Show Last 20 Lines |