opnform/app/Models/Workspace.php

172 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Models;
use App\Models\Forms\Form;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
2022-09-20 19:59:52 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Workspace extends Model implements CachableAttributes
2022-09-20 19:59:52 +00:00
{
2024-02-23 10:54:12 +00:00
use CachesAttributes;
use HasFactory;
public const MAX_FILE_SIZE_FREE = 5000000; // 5 MB
public const MAX_FILE_SIZE_PRO = 50000000; // 50 MB
public const MAX_DOMAIN_PRO = 1;
2022-09-20 19:59:52 +00:00
protected $fillable = [
'name',
'icon',
'user_id',
'custom_domain',
2022-09-20 19:59:52 +00:00
];
protected $appends = [
'is_pro',
2024-02-23 10:54:12 +00:00
'is_enterprise',
2022-09-20 19:59:52 +00:00
];
protected $casts = [
'custom_domains' => 'array',
];
protected $cachableAttributes = [
'is_pro',
'is_enterprise',
'is_risky',
'submissions_count',
'max_file_size',
2024-02-23 10:54:12 +00:00
'custom_domain_count',
];
2022-09-20 19:59:52 +00:00
public function getMaxFileSizeAttribute()
{
2024-02-23 10:54:12 +00:00
if (is_null(config('cashier.key'))) {
return self::MAX_FILE_SIZE_PRO;
}
2024-02-23 10:54:12 +00:00
return $this->remember('max_file_size', 15 * 60, function (): int {
// Return max file size depending on subscription
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
if ($license = $owner->activeLicense()) {
// In case of special License
return $license->max_file_size;
}
}
2024-02-23 10:54:12 +00:00
return self::MAX_FILE_SIZE_PRO;
}
return self::MAX_FILE_SIZE_FREE;
});
}
public function getCustomDomainCountLimitAttribute()
{
2024-02-23 10:54:12 +00:00
if (is_null(config('cashier.key'))) {
return null;
}
2024-02-23 10:54:12 +00:00
return $this->remember('custom_domain_count', 15 * 60, function (): ?int {
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
if ($license = $owner->activeLicense()) {
// In case of special License
return $license->custom_domain_limit_count;
}
2024-02-23 10:54:12 +00:00
2023-12-03 14:33:29 +00:00
return self::MAX_DOMAIN_PRO;
}
}
return 0;
});
}
public function getIsProAttribute()
{
2024-02-23 10:54:12 +00:00
if (is_null(config('cashier.key'))) {
return true; // If no paid plan so TRUE for ALL
}
2024-02-23 10:54:12 +00:00
return $this->remember('is_pro', 15 * 60, function (): bool {
// Make sure at least one owner is pro
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
return true;
}
}
2024-02-23 10:54:12 +00:00
return false;
});
}
2022-09-20 19:59:52 +00:00
public function getIsEnterpriseAttribute()
{
2024-02-23 10:54:12 +00:00
if (is_null(config('cashier.key'))) {
return true; // If no paid plan so TRUE for ALL
}
2022-09-20 19:59:52 +00:00
2024-02-23 10:54:12 +00:00
return $this->remember('is_enterprise', 15 * 60, function (): bool {
// Make sure at least one owner is pro
foreach ($this->owners as $owner) {
if ($owner->has_enterprise_subscription) {
return true;
}
2022-09-20 19:59:52 +00:00
}
2024-02-23 10:54:12 +00:00
return false;
});
2022-09-20 19:59:52 +00:00
}
public function getIsRiskyAttribute()
{
2024-02-23 10:54:12 +00:00
return $this->remember('is_risky', 15 * 60, function (): bool {
// A workspace is risky if all of his users are risky
foreach ($this->owners as $owner) {
2024-02-23 10:54:12 +00:00
if (! $owner->is_risky) {
return false;
}
}
return true;
});
}
public function getSubmissionsCountAttribute()
{
2024-02-23 10:54:12 +00:00
return $this->remember('submissions_count', 15 * 60, function (): int {
$total = 0;
foreach ($this->forms as $form) {
$total += $form->submissions_count;
}
return $total;
});
}
2022-09-20 19:59:52 +00:00
/**
* Relationships
*/
public function users()
{
return $this->belongsToMany(User::class);
}
public function owners()
{
return $this->users()->wherePivot('role', 'admin');
}
public function forms()
{
return $this->hasMany(Form::class);
}
}