ldc-web/app/Models/Project.php
2022-06-13 17:40:44 +02:00

186 lines
4 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'dev_login' => 'array',
'prod_login' => 'array'
];
// * 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 = ProjectsStatusChecker::select('updated_at')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->updated_at;
$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 = ProjectsStatusChecker::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 = ProjectsStatusChecker::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 = ProjectsStatusChecker::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 = ProjectsStatusChecker::select('code')->where('project_uuid', '=', $this->uuid)->first();
if (!$value) {
$value = 'missing';
} else {
$value = $value->code;
}
return $value;
}
/**
* get logs_hr attribute
*
* @return float
*/
public function getLogsHrAttribute(): float
{
$value = ProjectsStatusChecker::select('logs')->where('project_uuid', '=', $this->uuid)->first();
return $this->humanFilesize($value->logs);
}
/**
* Human readable file size
*
* @param float $size
* @return float
*/
private function humanFilesize(float $size): float
{
$i = floor(log($size, 1024));
return round($size / pow(1024, $i), [0,0,2,2,3][$i]) . [' O',' Ko',' Mo',' Go',' To'][$i];
}
}