lasthourcloud/app/Models/Project.php

147 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2024-07-09 08:45:10 +00:00
use OpenApi\Attributes as OA;
#[OA\Schema(
description: 'Project model',
type: 'object',
properties: [
'id' => ['type' => 'integer'],
'uuid' => ['type' => 'string'],
'name' => ['type' => 'string'],
'environments' => new OA\Property(
property: 'environments',
type: 'array',
items: new OA\Items(ref: '#/components/schemas/Environment'),
description: 'The environments of the project.'
),
]
)]
class Project extends BaseModel
{
2023-08-22 15:44:49 +00:00
protected $guarded = [];
2024-06-10 20:43:34 +00:00
public static function ownedByCurrentTeam()
{
2023-08-22 15:44:49 +00:00
return Project::whereTeamId(currentTeam()->id)->orderBy('name');
}
protected static function booted()
{
static::created(function ($project) {
ProjectSetting::create([
'project_id' => $project->id,
]);
2023-04-25 12:43:35 +00:00
Environment::create([
'name' => 'production',
2023-04-25 12:43:35 +00:00
'project_id' => $project->id,
]);
});
2023-10-05 06:46:26 +00:00
static::deleting(function ($project) {
$project->environments()->delete();
$project->settings()->delete();
2024-05-21 12:29:06 +00:00
$shared_variables = $project->environment_variables();
foreach ($shared_variables as $shared_variable) {
2024-06-10 20:43:34 +00:00
ray('Deleting project shared variable: '.$shared_variable->name);
2024-05-21 12:29:06 +00:00
$shared_variable->delete();
}
});
}
2024-06-10 20:43:34 +00:00
2024-03-07 10:35:00 +00:00
public function environment_variables()
{
2024-01-23 16:13:23 +00:00
return $this->hasMany(SharedEnvironmentVariable::class);
}
2024-06-10 20:43:34 +00:00
2023-04-25 12:43:35 +00:00
public function environments()
{
return $this->hasMany(Environment::class);
}
2023-04-25 12:43:35 +00:00
public function settings()
{
return $this->hasOne(ProjectSetting::class);
}
public function team()
{
return $this->belongsTo(Team::class);
}
2024-03-07 10:14:03 +00:00
public function services()
{
return $this->hasManyThrough(Service::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2023-04-25 12:43:35 +00:00
public function applications()
{
2023-03-30 17:50:27 +00:00
return $this->hasManyThrough(Application::class, Environment::class);
}
2023-08-07 16:46:40 +00:00
public function postgresqls()
{
2023-08-07 20:14:21 +00:00
return $this->hasManyThrough(StandalonePostgresql::class, Environment::class);
2023-08-07 16:46:40 +00:00
}
2024-06-10 20:43:34 +00:00
2023-10-12 15:18:33 +00:00
public function redis()
{
return $this->hasManyThrough(StandaloneRedis::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function keydbs()
{
return $this->hasManyThrough(StandaloneKeydb::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function dragonflies()
{
return $this->hasManyThrough(StandaloneDragonfly::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function clickhouses()
{
return $this->hasManyThrough(StandaloneClickhouse::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
public function mongodbs()
{
return $this->hasManyThrough(StandaloneMongodb::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
public function mysqls()
{
2024-01-31 13:18:59 +00:00
return $this->hasManyThrough(StandaloneMysql::class, Environment::class);
2023-10-24 12:31:28 +00:00
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
public function mariadbs()
{
2024-01-31 13:18:59 +00:00
return $this->hasManyThrough(StandaloneMariadb::class, Environment::class);
}
2024-06-10 20:43:34 +00:00
2024-03-07 10:35:00 +00:00
public function resource_count()
{
2024-07-23 09:36:05 +00:00
return $this->applications()->count() + $this->postgresqls()->count() + $this->redis()->count() + $this->mongodbs()->count() + $this->mysqls()->count() + $this->mariadbs()->count() + $this->keydbs()->count() + $this->dragonflies()->count() + $this->clickhouses()->count() + $this->services()->count();
2023-10-24 12:31:28 +00:00
}
2024-06-10 20:43:34 +00:00
public function databases()
{
2024-05-21 12:29:06 +00:00
return $this->postgresqls()->get()->merge($this->redis()->get())->merge($this->mongodbs()->get())->merge($this->mysqls()->get())->merge($this->mariadbs()->get())->merge($this->keydbs()->get())->merge($this->dragonflies()->get())->merge($this->clickhouses()->get());
}
public function default_environment()
{
$default = $this->environments()->where('name', 'production')->first();
if ($default) {
return $default->name;
}
$default = $this->environments()->get();
if ($default->count() > 0) {
return $default->sortBy('created_at')->first()->name;
}
return null;
}
2023-08-07 20:14:21 +00:00
}