Cached some model properties and remove useless eager loading (#253)
* Cached some model properties and remove useless eager loading * Remove ray call * Remove double loading of forms * Add disableCache feature when needed
This commit is contained in:
@@ -4,12 +4,14 @@ namespace App\Models;
|
||||
|
||||
use App\Http\Requests\AnswerFormRequest;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Traits\CachableAttributes;
|
||||
use App\Models\Traits\CachesAttributes;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Workspace extends Model
|
||||
class Workspace extends Model implements CachableAttributes
|
||||
{
|
||||
use HasFactory;
|
||||
use HasFactory, CachesAttributes;
|
||||
|
||||
const MAX_FILE_SIZE_FREE = 5000000; // 5 MB
|
||||
const MAX_FILE_SIZE_PRO = 50000000; // 50 MB
|
||||
@@ -32,20 +34,14 @@ class Workspace extends Model
|
||||
'custom_domains' => 'array',
|
||||
];
|
||||
|
||||
public function getIsProAttribute()
|
||||
{
|
||||
if(is_null(config('cashier.key'))){
|
||||
return true; // If no paid plan so TRUE for ALL
|
||||
}
|
||||
|
||||
// Make sure at least one owner is pro
|
||||
foreach ($this->owners as $owner) {
|
||||
if ($owner->is_subscribed) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected $cachableAttributes = [
|
||||
'is_pro',
|
||||
'is_enterprise',
|
||||
'is_risky',
|
||||
'submissions_count',
|
||||
'max_file_size',
|
||||
'custom_domain_count'
|
||||
];
|
||||
|
||||
public function getMaxFileSizeAttribute()
|
||||
{
|
||||
@@ -53,18 +49,20 @@ class Workspace extends Model
|
||||
return self::MAX_FILE_SIZE_PRO;
|
||||
}
|
||||
|
||||
// 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;
|
||||
return $this->remember('max_file_size', 15, 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;
|
||||
}
|
||||
}
|
||||
return self::MAX_FILE_SIZE_PRO;
|
||||
}
|
||||
return self::MAX_FILE_SIZE_PRO;
|
||||
}
|
||||
|
||||
return self::MAX_FILE_SIZE_FREE;
|
||||
return self::MAX_FILE_SIZE_FREE;
|
||||
});
|
||||
}
|
||||
|
||||
public function getCustomDomainCountLimitAttribute()
|
||||
@@ -73,18 +71,36 @@ class Workspace extends Model
|
||||
return null;
|
||||
}
|
||||
|
||||
// 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->custom_domain_limit_count;
|
||||
return $this->remember('custom_domain_count', 15, 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;
|
||||
}
|
||||
}
|
||||
return self::MAX_DOMAIN_PRO;
|
||||
}
|
||||
return self::MAX_DOMAIN_PRO;
|
||||
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
public function getIsProAttribute()
|
||||
{
|
||||
if(is_null(config('cashier.key'))){
|
||||
return true; // If no paid plan so TRUE for ALL
|
||||
}
|
||||
|
||||
return 0;
|
||||
return $this->remember('is_pro', 15, function(): bool {
|
||||
// Make sure at least one owner is pro
|
||||
foreach ($this->owners as $owner) {
|
||||
if ($owner->is_subscribed) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public function getIsEnterpriseAttribute()
|
||||
@@ -93,34 +109,41 @@ class Workspace extends Model
|
||||
return true; // If no paid plan so TRUE for ALL
|
||||
}
|
||||
|
||||
foreach ($this->owners as $owner) {
|
||||
if ($owner->has_enterprise_subscription) {
|
||||
return true;
|
||||
return $this->remember('is_enterprise', 15, function(): bool {
|
||||
// Make sure at least one owner is pro
|
||||
foreach ($this->owners as $owner) {
|
||||
if ($owner->has_enterprise_subscription) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public function getIsRiskyAttribute()
|
||||
{
|
||||
// A workspace is risky if all of his users are risky
|
||||
foreach ($this->owners as $owner) {
|
||||
if (!$owner->is_risky) {
|
||||
return false;
|
||||
return $this->remember('is_risky', 15, function(): bool {
|
||||
// A workspace is risky if all of his users are risky
|
||||
foreach ($this->owners as $owner) {
|
||||
if (!$owner->is_risky) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function getSubmissionsCountAttribute()
|
||||
{
|
||||
$total = 0;
|
||||
foreach ($this->forms as $form) {
|
||||
$total += $form->submissions_count;
|
||||
}
|
||||
return $this->remember('submissions_count', 15, function(): int {
|
||||
$total = 0;
|
||||
foreach ($this->forms as $form) {
|
||||
$total += $form->submissions_count;
|
||||
}
|
||||
|
||||
return $total;
|
||||
return $total;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user