This commit is contained in:
Saeed Vaziry
2024-09-27 20:36:03 +02:00
committed by GitHub
parent b62c40c97d
commit f6bc04763b
122 changed files with 6609 additions and 807 deletions

View File

@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;
/**
* @property int $user_id
@ -15,6 +16,9 @@
* @property bool $connected
* @property User $user
* @property ?int $project_id
* @property Server[] $servers
* @property Project $project
* @property string $image_url
*/
class ServerProvider extends AbstractModel
{
@ -51,6 +55,13 @@ public function servers(): HasMany
return $this->hasMany(Server::class, 'provider_id');
}
public function provider(): \App\ServerProviders\ServerProvider
{
$providerClass = config('core.server_providers_class')[$this->provider];
return new $providerClass($this);
}
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
@ -59,7 +70,54 @@ public function project(): BelongsTo
public static function getByProjectId(int $projectId): Builder
{
return self::query()
->where('project_id', $projectId)
->orWhereNull('project_id');
->where(function (Builder $query) use ($projectId) {
$query->where('project_id', $projectId)
->orWhereNull('project_id');
});
}
public function getImageUrlAttribute(): string
{
return url('/static/images/'.$this->provider.'.svg');
}
public static function regions(?int $id): array
{
if (! $id) {
return [];
}
$profile = self::find($id);
if (! $profile) {
return [];
}
if (Cache::get('regions-'.$id)) {
return Cache::get('regions-'.$id);
}
$regions = $profile->provider()->regions();
Cache::put('regions-'.$id, $regions, 600);
return $regions;
}
public static function plans(?int $id, ?string $region): array
{
if (! $id) {
return [];
}
$profile = self::find($id);
if (! $profile) {
return [];
}
if (Cache::get('plans-'.$id.'-'.$region)) {
return Cache::get('plans-'.$id.'-'.$region);
}
$plans = $profile->provider()->plans($region);
Cache::put('plans-'.$id.'-'.$region, $plans, 600);
return $plans;
}
}