Page MenuHomePhorge

Entitlement.php
No OneTemporary

Entitlement.php

<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* The eloquent definition of an Entitlement.
*
* Owned by a {@link \App\User}, billed to a {@link \App\Wallet}.
*
* @property \App\User $owner The owner of this entitlement (subject).
* @property \App\Sku $sku The SKU to which this entitlement applies.
* @property \App\Wallet $wallet The wallet to which this entitlement is charged.
* @property \App\Domain|\App\User $entitleable The entitled object (receiver of the entitlement).
*/
class Entitlement extends Model
{
use SoftDeletes;
/**
* This table does not use auto-increment.
*
* @var boolean
*/
public $incrementing = false;
/**
* The key type is actually a string.
*
* @var string
*/
protected $keyType = 'string';
/**
* The fillable columns for this Entitlement
*
* @var array
*/
protected $fillable = [
'sku_id',
'wallet_id',
'entitleable_id',
'entitleable_type',
'cost',
'description'
];
protected $casts = [
'cost' => 'integer',
];
/**
* Return the costs per day for this entitlement.
*
* @return float
*/
public function costsPerDay()
{
if ($this->cost == 0) {
return (float) 0;
}
$discount = $this->wallet->getDiscountRate();
$daysInLastMonth = \App\Utils::daysInLastMonth();
$costsPerDay = (float) ($this->cost * $discount) / $daysInLastMonth;
return $costsPerDay;
}
/**
* Create a transaction record for this entitlement.
*
* @param string $type The type of transaction ('created', 'billed', 'deleted'), but use the
* \App\Transaction constants.
* @param int $amount The amount involved in cents
*
* @return string The transaction ID
*/
public function createTransaction($type, $amount = null)
{
$transaction = \App\Transaction::create(
[
'object_id' => $this->id,
'object_type' => \App\Entitlement::class,
'type' => $type,
'amount' => $amount
]
);
return $transaction->id;
}
/**
* Principally entitleable objects such as 'Domain' or 'User'.
*
* @return mixed
*/
public function entitleable()
{
return $this->morphTo();
}
/**
* Returns entitleable object title (e.g. email or domain name).
*
* @return string|null An object title/name
*/
public function entitleableTitle(): ?string
{
if ($this->entitleable instanceof \App\User) {
return $this->entitleable->email;
}
if ($this->entitleable instanceof \App\Domain) {
return $this->entitleable->namespace;
}
}
/**
* The SKU concerned.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function sku()
{
return $this->belongsTo('App\Sku');
}
/**
* The wallet this entitlement is being billed to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function wallet()
{
return $this->belongsTo('App\Wallet');
}
/**
* Cost mutator. Make sure cost is integer.
*/
public function setCostAttribute($cost): void
{
$this->attributes['cost'] = round($cost);
}
}

File Metadata

Mime Type
text/x-php
Expires
Mon, Aug 25, 4:40 PM (20 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
225451
Default Alt Text
Entitlement.php (3 KB)

Event Timeline