Changeset View
Changeset View
Standalone View
Standalone View
src/app/Entitlement.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Carbon\Carbon; | |||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
use App\Traits\UuidStrKeyTrait; | 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}. | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | public static function objectEntitlementsSummary($object): array | ||||
$skus[$sku->id]['count']++; | $skus[$sku->id]['count']++; | ||||
$skus[$sku->id]['costs'][] = $ent->cost; | $skus[$sku->id]['costs'][] = $ent->cost; | ||||
} | } | ||||
return $skus; | return $skus; | ||||
} | } | ||||
/** | /** | ||||
* Restore object entitlements. | |||||
* | |||||
* @param \App\User|\App\Domain|\App\Group $object The user|domain|group object | |||||
*/ | |||||
public static function restoreEntitlementsFor($object): void | |||||
{ | |||||
// We'll restore only these that were deleted last. So, first we get | |||||
// the maximum deleted_at timestamp and then use it to select | |||||
// entitlements for restore | |||||
$deleted_at = $object->entitlements()->withTrashed()->max('deleted_at'); | |||||
if ($deleted_at) { | |||||
$threshold = (new \Carbon\Carbon($deleted_at))->subMinute(); | |||||
// Restore object entitlements | |||||
$object->entitlements()->withTrashed() | |||||
->where('deleted_at', '>=', $threshold) | |||||
->update(['updated_at' => now(), 'deleted_at' => null]); | |||||
// Note: We're assuming that cost of entitlements was correct | |||||
// on deletion, so we don't have to re-calculate it again. | |||||
// TODO: We should probably re-calculate the cost | |||||
} | |||||
} | |||||
/** | |||||
* 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('App\Sku'); | ||||
} | } | ||||
Show All 19 Lines |