Refactor realValue() method to include resource lookup

This commit is contained in:
Andras Bacsai
2024-01-24 15:54:55 +01:00
parent 7d754558b0
commit 348923ae02

View File

@@ -49,10 +49,32 @@ class EnvironmentVariable extends Model
set: fn (?string $value = null) => $this->set_environment_variables($value), set: fn (?string $value = null) => $this->set_environment_variables($value),
); );
} }
protected function realValue(): Attribute public function realValue(): Attribute
{ {
$resource = null;
if ($this->application_id) {
$resource = Application::find($this->application_id);
} else if ($this->service_id) {
$resource = Service::find($this->service_id);
} else if ($this->database_id) {
$resource = StandalonePostgresql::find($this->database_id);
if (!$resource) {
$resource = StandaloneMysql::find($this->database_id);
if (!$resource) {
$resource = StandaloneRedis::find($this->database_id);
if (!$resource) {
$resource = StandaloneMongodb::find($this->database_id);
if (!$resource) {
$resource = StandaloneMariadb::find($this->database_id);
}
}
}
}
}
return Attribute::make( return Attribute::make(
get: fn () => $this->get_real_environment_variables($this->value), get: function () use ($resource) {
return $this->get_real_environment_variables($this->value, $resource);
}
); );
} }
protected function isShared(): Attribute protected function isShared(): Attribute
@@ -112,7 +134,7 @@ class EnvironmentVariable extends Model
} }
} }
} }
private function get_real_environment_variables(?string $environment_variable = null): string|null private function get_real_environment_variables(?string $environment_variable = null, $resource = null): string|null
{ {
if (!$environment_variable) { if (!$environment_variable) {
return null; return null;
@@ -123,7 +145,14 @@ class EnvironmentVariable extends Model
$variable = Str::after($environment_variable, "{$type}."); $variable = Str::after($environment_variable, "{$type}.");
$variable = Str::before($variable, '}}'); $variable = Str::before($variable, '}}');
$variable = Str::of($variable)->trim()->value; $variable = Str::of($variable)->trim()->value;
$environment_variable_found = SharedEnvironmentVariable::where("type", $type)->where('key', $variable)->where('team_id', $this->team()->id)->first(); if ($type === 'environment') {
$id = $resource->environment->id;
} else if ($type === 'project') {
$id = $resource->environment->project->id;
} else {
$id = $this->team()->id;
}
$environment_variable_found = SharedEnvironmentVariable::where("type", $type)->where('key', $variable)->where('team_id', $this->team()->id)->where("{$type}_id", $id)->first();
if ($environment_variable_found) { if ($environment_variable_found) {
return $environment_variable_found->value; return $environment_variable_found->value;
} }