Changeset View
Changeset View
Standalone View
Standalone View
src/app/Group.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Traits\BelongsToTenantTrait; | use App\Traits\BelongsToTenantTrait; | ||||
use App\Traits\EntitleableTrait; | |||||
use App\Traits\UuidIntKeyTrait; | use App\Traits\UuidIntKeyTrait; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
/** | /** | ||||
* The eloquent definition of a Group. | * The eloquent definition of a Group. | ||||
* | * | ||||
* @property int $id The group identifier | * @property int $id The group identifier | ||||
* @property string $email An email address | * @property string $email An email address | ||||
* @property string $members A comma-separated list of email addresses | * @property string $members A comma-separated list of email addresses | ||||
* @property int $status The group status | * @property int $status The group status | ||||
* @property int $tenant_id Tenant identifier | * @property int $tenant_id Tenant identifier | ||||
*/ | */ | ||||
class Group extends Model | class Group extends Model | ||||
{ | { | ||||
use BelongsToTenantTrait; | use BelongsToTenantTrait; | ||||
use UuidIntKeyTrait; | use EntitleableTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
use UuidIntKeyTrait; | |||||
// we've simply never heard of this domain | // we've simply never heard of this domain | ||||
public const STATUS_NEW = 1 << 0; | public const STATUS_NEW = 1 << 0; | ||||
// it's been activated | // it's been activated | ||||
public const STATUS_ACTIVE = 1 << 1; | public const STATUS_ACTIVE = 1 << 1; | ||||
// domain has been suspended. | // domain has been suspended. | ||||
public const STATUS_SUSPENDED = 1 << 2; | public const STATUS_SUSPENDED = 1 << 2; | ||||
// domain has been deleted | // domain has been deleted | ||||
Show All 16 Lines | class Group extends Model | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public function assignToWallet(Wallet $wallet): Group | public function assignToWallet(Wallet $wallet): Group | ||||
{ | { | ||||
if (empty($this->id)) { | if (empty($this->id)) { | ||||
throw new \Exception("Group not yet exists"); | throw new \Exception("Group not yet exists"); | ||||
} | } | ||||
if ($this->entitlement()->count()) { | if ($this->entitlements()->count()) { | ||||
throw new \Exception("Group already assigned to a wallet"); | throw new \Exception("Group already assigned to a wallet"); | ||||
} | } | ||||
$sku = \App\Sku::withObjectTenantContext($this)->where('title', 'group')->first(); | $sku = \App\Sku::withObjectTenantContext($this)->where('title', 'group')->first(); | ||||
$exists = $wallet->entitlements()->where('sku_id', $sku->id)->count(); | $exists = $wallet->entitlements()->where('sku_id', $sku->id)->count(); | ||||
\App\Entitlement::create([ | \App\Entitlement::create([ | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
Show All 40 Lines | public static function emailExists(string $email, bool $return_group = false) | ||||
if ($group) { | if ($group) { | ||||
return $return_group ? $group : true; | return $return_group ? $group : true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* The group entitlement. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\MorphOne | |||||
*/ | |||||
public function entitlement() | |||||
{ | |||||
return $this->morphOne('App\Entitlement', 'entitleable'); | |||||
} | |||||
/** | |||||
* Group members propert accessor. Converts internal comma-separated list into an array | * Group members propert accessor. Converts internal comma-separated list into an array | ||||
* | * | ||||
* @param string $members Comma-separated list of email addresses | * @param string $members Comma-separated list of email addresses | ||||
* | * | ||||
* @return array Email addresses of the group members, as an array | * @return array Email addresses of the group members, as an array | ||||
*/ | */ | ||||
public function getMembersAttribute($members): array | public function getMembersAttribute($members): array | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | class Group extends Model | ||||
{ | { | ||||
if (!$this->isSuspended()) { | if (!$this->isSuspended()) { | ||||
return; | return; | ||||
} | } | ||||
$this->status ^= Group::STATUS_SUSPENDED; | $this->status ^= Group::STATUS_SUSPENDED; | ||||
$this->save(); | $this->save(); | ||||
} | } | ||||
/** | |||||
* Returns the wallet by which the group is controlled | |||||
* | |||||
* @return \App\Wallet A wallet object | |||||
*/ | |||||
public function wallet(): ?Wallet | |||||
{ | |||||
// Note: Not all domains have a entitlement/wallet | |||||
$entitlement = $this->entitlement()->withTrashed()->orderBy('created_at', 'desc')->first(); | |||||
return $entitlement ? $entitlement->wallet : null; | |||||
} | |||||
} | } |