Changeset View
Changeset View
Standalone View
Standalone View
src/app/Entitlement.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Traits\UuidStrKeyTrait; | |||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
use App\Traits\UuidStrKeyTrait; | |||||
/** | /** | ||||
* The eloquent definition of an Entitlement. | * The eloquent definition of an Entitlement. | ||||
* | * | ||||
* Owned by a {@link \App\User}, billed to a {@link \App\Wallet}. | * Owned by a {@link \App\User}, billed to a {@link \App\Wallet}. | ||||
* | * | ||||
* @property int $cost | * @property int $cost | ||||
* @property ?string $description | * @property ?string $description | ||||
* @property \App\Domain|\App\User $entitleable The entitled object (receiver of the entitlement). | * @property \App\Domain|\App\User $entitleable The entitled object (receiver of the entitlement). | ||||
* @property int $entitleable_id | * @property int $entitleable_id | ||||
* @property string $entitleable_type | * @property string $entitleable_type | ||||
* @property int $fee | * @property int $fee | ||||
* @property string $id | * @property string $id | ||||
* @property \App\User $owner The owner of this entitlement (subject). | * @property \App\User $owner The owner of this entitlement (subject). | ||||
* @property \App\Sku $sku The SKU to which this entitlement applies. | * @property \App\Sku $sku The SKU to which this entitlement applies. | ||||
* @property string $sku_id | * @property string $sku_id | ||||
* @property \App\Wallet $wallet The wallet to which this entitlement is charged. | * @property \App\Wallet $wallet The wallet to which this entitlement is charged. | ||||
* @property string $wallet_id | * @property string $wallet_id | ||||
*/ | */ | ||||
class Entitlement extends Model | class Entitlement extends Model | ||||
{ | { | ||||
use SoftDeletes; | use SoftDeletes; | ||||
use UuidStrKeyTrait; | use UuidStrKeyTrait; | ||||
/** | /** @var array<int, string> The attributes that are mass assignable */ | ||||
* The fillable columns for this Entitlement | |||||
* | |||||
* @var array | |||||
*/ | |||||
protected $fillable = [ | protected $fillable = [ | ||||
'sku_id', | 'sku_id', | ||||
'wallet_id', | 'wallet_id', | ||||
'entitleable_id', | 'entitleable_id', | ||||
'entitleable_type', | 'entitleable_type', | ||||
'cost', | 'cost', | ||||
'description', | 'description', | ||||
'fee', | 'fee', | ||||
]; | ]; | ||||
/** @var array<string, string> The attributes that should be cast */ | |||||
protected $casts = [ | protected $casts = [ | ||||
'cost' => 'integer', | 'cost' => 'integer', | ||||
'fee' => 'integer' | 'fee' => 'integer' | ||||
]; | ]; | ||||
/** | /** | ||||
* Return the costs per day for this entitlement. | * Return the costs per day for this entitlement. | ||||
* | * | ||||
Show All 20 Lines | class Entitlement extends Model | ||||
* @param string $type The type of transaction ('created', 'billed', 'deleted'), but use the | * @param string $type The type of transaction ('created', 'billed', 'deleted'), but use the | ||||
* \App\Transaction constants. | * \App\Transaction constants. | ||||
* @param int $amount The amount involved in cents | * @param int $amount The amount involved in cents | ||||
* | * | ||||
* @return string The transaction ID | * @return string The transaction ID | ||||
*/ | */ | ||||
public function createTransaction($type, $amount = null) | public function createTransaction($type, $amount = null) | ||||
{ | { | ||||
$transaction = \App\Transaction::create( | $transaction = Transaction::create( | ||||
[ | [ | ||||
'object_id' => $this->id, | 'object_id' => $this->id, | ||||
'object_type' => \App\Entitlement::class, | 'object_type' => Entitlement::class, | ||||
'type' => $type, | 'type' => $type, | ||||
'amount' => $amount | 'amount' => $amount | ||||
] | ] | ||||
); | ); | ||||
return $transaction->id; | return $transaction->id; | ||||
} | } | ||||
Show All 10 Lines | class Entitlement extends Model | ||||
/** | /** | ||||
* Returns entitleable object title (e.g. email or domain name). | * Returns entitleable object title (e.g. email or domain name). | ||||
* | * | ||||
* @return string|null An object title/name | * @return string|null An object title/name | ||||
*/ | */ | ||||
public function entitleableTitle(): ?string | public function entitleableTitle(): ?string | ||||
{ | { | ||||
if ($this->entitleable instanceof \App\Domain) { | if ($this->entitleable instanceof Domain) { | ||||
return $this->entitleable->namespace; | return $this->entitleable->namespace; | ||||
} | } | ||||
return $this->entitleable->email; | return $this->entitleable->email; | ||||
} | } | ||||
/** | /** | ||||
* Simplified Entitlement/SKU information for a specified entitleable object | * Simplified Entitlement/SKU information for a specified entitleable object | ||||
Show All 24 Lines | class Entitlement extends Model | ||||
/** | /** | ||||
* The SKU concerned. | * The SKU concerned. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||||
*/ | */ | ||||
public function sku() | public function sku() | ||||
{ | { | ||||
return $this->belongsTo('App\Sku'); | return $this->belongsTo(Sku::class); | ||||
} | } | ||||
/** | /** | ||||
* The wallet this entitlement is being billed to | * The wallet this entitlement is being billed to | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||||
*/ | */ | ||||
public function wallet() | public function wallet() | ||||
{ | { | ||||
return $this->belongsTo('App\Wallet'); | return $this->belongsTo(Wallet::class); | ||||
} | } | ||||
/** | /** | ||||
* Cost mutator. Make sure cost is integer. | * Cost mutator. Make sure cost is integer. | ||||
*/ | */ | ||||
public function setCostAttribute($cost): void | public function setCostAttribute($cost): void | ||||
{ | { | ||||
$this->attributes['cost'] = round($cost); | $this->attributes['cost'] = round($cost); | ||||
} | } | ||||
} | } |