Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/EntitleableTrait.php
Show First 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | protected static function bootEntitleableTrait() | ||||
// Restore object's entitlements on restore | // Restore object's entitlements on restore | ||||
static::restored(function ($model) { | static::restored(function ($model) { | ||||
$model->restoreEntitlements(); | $model->restoreEntitlements(); | ||||
}); | }); | ||||
} | } | ||||
/** | /** | ||||
* Count entitlements for the specified SKU. | |||||
* | |||||
* @param string $title The SKU title | |||||
* | |||||
* @return int Numer of entitlements | |||||
*/ | |||||
public function countEntitlementsBySku(string $title): int | |||||
{ | |||||
$sku = $this->skuByTitle($title); | |||||
if (!$sku) { | |||||
return 0; | |||||
} | |||||
return $this->entitlements()->where('sku_id', $sku->id)->count(); | |||||
} | |||||
/** | |||||
* Entitlements for this object. | * Entitlements for this object. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function entitlements() | public function entitlements() | ||||
{ | { | ||||
return $this->hasMany(Entitlement::class, 'entitleable_id', 'id') | return $this->hasMany(Entitlement::class, 'entitleable_id', 'id') | ||||
->where('entitleable_type', self::class); | ->where('entitleable_type', self::class); | ||||
} | } | ||||
/** | /** | ||||
* 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 = $this->skuByTitle($title); | return $this->countEntitlementsBySku($title) > 0; | ||||
if (!$sku) { | |||||
return false; | |||||
} | |||||
return $this->entitlements()->where('sku_id', $sku->id)->count() > 0; | |||||
} | } | ||||
/** | /** | ||||
* Remove a number of entitlements for the SKU. | * Remove a number of entitlements for the SKU. | ||||
* | * | ||||
* @param \App\Sku $sku The SKU | * @param \App\Sku $sku The SKU | ||||
* @param int $count The number of entitlements to remove | * @param int $count The number of entitlements to remove | ||||
* | * | ||||
▲ Show 20 Lines • Show All 117 Lines • Show Last 20 Lines |