ldc-web/app/Models/Project.php
2022-12-22 10:36:32 +01:00

259 lines
6.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\PhpVersion;
use App\Enums\ProjectType;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Project extends Model
{
use CastsEnums;
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'dev_login' => 'encrypted:array',
'prod_login' => 'encrypted:array',
'prod_document_root' => 'encrypted',
'dev_document_root' => 'encrypted',
'prod_unix_user' => 'encrypted',
'prod_unix_pwd' => 'encrypted',
'dev_unix_user' => 'encrypted',
'dev_unix_pwd' => 'encrypted',
'prod_db_user' => 'encrypted',
'prod_db_pwd' => 'encrypted',
'dev_db_user' => 'encrypted',
'dev_db_pwd' => 'encrypted',
'prod_db_schema' => 'encrypted',
'dev_db_schema' => 'encrypted',
'prod_comment' => 'encrypted',
'dev_comment' => 'encrypted',
'prod_eai' => 'encrypted',
'dev_eai' => 'encrypted',
'prod_whitelist_fw' => 'encrypted',
'dev_whitelist_fw' => 'encrypted',
'dev_htaccess_user' => 'encrypted',
'dev_htaccess_pwd' => 'encrypted',
'prod_ga_id' => 'encrypted',
'prod_laravel_app_key' => 'encrypted',
'dev_laravel_app_key' => 'encrypted',
'prod_smtp_config' => 'encrypted',
'prod_smtp_key' => 'encrypted',
'prod_haproxy_id' => 'encrypted',
'dev_haproxy_id' => 'encrypted',
'https' => 'boolean'
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'php' => PhpVersion::class,
'type' => ProjectType::class,
'smtp' => SmtpEnum::class
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'display_name', 'php', 'type', 'https', 'prod_haproxy_id',
'dev_haproxy_id', 'prod_url', 'dev_url', 'prod_bo_url', 'dev_bo_url', 'prod_login', 'dev_login',
'prod_document_root', 'dev_document_root', 'prod_unix_user', 'dev_unix_user',
'prod_unix_pwd', 'dev_unix_pwd', 'prod_db_user', 'dev_db_user',
'prod_db_pwd', 'dev_db_pwd', 'prod_db_schema', 'dev_db_schema',
'prod_db_server_id', 'dev_db_server_id', 'prod_web_server_id',
'dev_web_server_id', 'prod_comment', 'dev_comment', 'prod_eai',
'dev_eai', 'prod_whitelist_fw', 'dev_whitelist_fw',
'dev_htaccess_user', 'dev_htaccess_pwd', 'prod_ga_id',
'prod_laravel_app_key', 'dev_laravel_app_key',
'prod_smtp_config', 'prod_smtp_key',
];
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName(): string
{
return 'uuid';
}
// * METHODS
/**
* get dev_web_ip attribute
*
* @return mixed
*/
public function getDevWebIpAttribute()
{
$value = Server::find($this->dev_web_server_id);
if ($value) {
$value = $value->ssh;
}
return $value;
}
/**
* get dev_db_ip attribute
*
* @return mixed
*/
public function getDevDbIpAttribute()
{
$value = Server::find($this->dev_db_server_id);
if ($value) {
$value = $value->ssh;
}
return $value;
}
/**
* get prod_web_ip attribute
*
* @return mixed
*/
public function getProdWebIpAttribute()
{
$value = Server::find($this->prod_web_server_id);
if ($value) {
$value = $value->ssh;
}
return $value;
}
/**
* get prod_db_ip attribute
*
* @return mixed
*/
public function getProdDbIpAttribute()
{
$value = Server::find($this->prod_db_server_id);
if ($value) {
$value = $value->ssh;
}
return $value;
}
/**
* get checked attribute
*
* @return mixed
*/
public function getCheckedAttribute()
{
$value = ProjectStatusChecker::select('last_call')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->last_call;
$dt = Carbon::now();
if ($dt->diffInMinutes($value) > 30) {
$value = 'false';
} else {
$value = 'true';
}
}
return $value;
}
/**
* get sitemap_checked attribute
*
* @return mixed
*/
public function getSitemapCheckedAttribute()
{
$value = ProjectStatusChecker::select('sitemap')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->sitemap;
}
return $value;
}
/**
* get robots_checked attribute
*
* @return mixed
*/
public function getRobotsCheckedAttribute()
{
$value = ProjectStatusChecker::select('robots')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->robots;
}
return $value;
}
/**
* get logs_checked attribute
*
* @return mixed
*/
public function getLogsCheckedAttribute()
{
$value = ProjectStatusChecker::select('logs')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->logs;
}
return $value;
}
/**
* get code attribute
*
* @return mixed
*/
public function getCodeAttribute()
{
$value = ProjectStatusChecker::select('code')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->code;
}
return $value;
}
/**
* get logs_hr attribute
*
* @return string
*/
public function getLogsHrAttribute(): string
{
$value = ProjectStatusChecker::select('logs')->where('project_uuid', '=', $this->uuid)->first();
return $this->humanFilesize($value->logs);
}
/**
* Human readable file size
*
* @param float $size
* @return string
*/
private function humanFilesize(float $size): string
{
$i = \floatval(floor(log($size, 1024)));
return round($size / pow(1024, $i), [0,0,2,2,3][$i]) . [' O',' Ko',' Mo',' Go',' To'][$i];
}
}