ldc-projects-administration/app/Lib/CdEnvVariable.php
2024-01-25 17:29:29 +01:00

209 lines
7 KiB
PHP

<?php
namespace App\Lib;
use App\Enums\EnvironmentScope;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
/**
* @property-read \App\Enums\EnvironmentScope $environment_scope The scope on which the variable applies.
* @property-read string $key The variable name.
* @property-read string $value The variable value.
* @property-read boolean $protected Export variable to pipelines running on
* protected branches and tags only.
* @property-read boolean $masked Variable will be masked in job logs.
* Requires values to meet regular expression
* requirements.
* @property-read boolean $raw Char '$' will be treated as the start of a reference
* to another variable.
* @property-read boolean $isDefault Variable is needed for all projects.
* @package App\Lib
*/
class CdEnvVariable implements \Stringable
{
/**
* The k8s prefix
*
* @var string
*/
private static string $k8sPrefix = 'APP';
/**
* The prefix to use for defaults en vars
*
* @var string
*/
private static string $prefix = 'LRVL';
/**
* The variable is default
*
* @var boolean
*/
private bool $isDefault = false;
/**
* Properties that can be accessed from the object.
*
* @var array<string>
*/
private static array $readableProps = [
'environment_scope', 'key', 'value', 'protected',
'masked', 'raw', 'isDefault',
];
/**
* CdEnvVariable
*
* @param \App\Enums\EnvironmentScope $environment_scope The scope on which the variable applies.
* @param string $key The variable name.
* @param string $value The variable value.
* @param boolean $protected Export variable to pipelines running on
* protected branches and tags only.
* @param boolean $masked Variable will be masked in job logs.
* Requires values to meet regular expression requirements.
* @param boolean $raw Char '$' will be treated as the start of a reference
* to another variable.
* @param string $variable_type Should always be 'env_var' for now.
* @return void
*/
public function __construct(
protected EnvironmentScope $environment_scope,
protected string $key,
protected string $value,
protected bool $protected = false,
protected bool $masked = false,
protected bool $raw = false,
protected string $variable_type = 'env_var',
) {
}
/**
* Make a CdEnvVariable
*
* @param array<string|boolean|\App\Enums\EnvironmentScope> $params
* @return self
*/
public static function make(
array $params
): self {
self::removeVarNamePrefixes($params);
return new self(...$params);
}
/**
* Property accessor
*
* @param string $name
* @return boolean|string|\App\Enums\EnvironmentScope
* @throws \Error If the property is not accessible.
*/
public function __get(string $name): bool|string|EnvironmentScope
{
if (\in_array($name, self::$readableProps)) {
return $this->{$name};
}
throw new \Error('No property ' . __CLASS__ . "::{$name}");
}
/**
* Return is default.
*
* @return self
*/
public function isProjectDefault(): self
{
$this->isDefault = true;
return $this;
}
/**
* Normalize var name
*
* @return string
*/
public function getFormatedGitlabKey(): string
{
// * Format with wanted prefixes
if ($this->isDefault) {
return sprintf(
'%s_%s_%s',
self::$k8sPrefix,
self::$prefix,
Str::upper($this->key)
);
}
return sprintf(
'%s_%s',
self::$k8sPrefix,
str::upper($this->key)
);
}
// * PRIVATE
/**
* Remove var name prefixes
*
* @param array<string|boolean|\App\Enums\EnvironmentScope> $params
* @return void
*/
private static function removeVarNamePrefixes(array &$params): void
{
// * Force remove k8sPrefix
if (Str::startsWith(Arr::get($params, 'key'), self::$k8sPrefix)) {
Arr::set($params, 'key', Str::replaceStart(self::$k8sPrefix . '_', '', Arr::get($params, 'key')));
}
// * Force remove prefix
if (Str::startsWith(Arr::get($params, 'key'), self::$prefix)) {
Arr::set($params, 'key', Str::replaceStart(self::$prefix . '_', '', Arr::get($params, 'key')));
}
}
/**
* String representation of the env variable
*
* @return string
*/
public function __toString(): string
{
$rowLength = 40;
$titlePad = 20;
$printStr = Str::of('')->newLine();
// * TOP ROW
$printStr = $printStr->append(\str_pad(" VAR `{$this->key}` ", $rowLength, '_', \STR_PAD_BOTH))->newLine();
$printStr = $printStr->append(\str_pad('value', $titlePad, pad_type: \STR_PAD_LEFT))
->append(' => ');
$value = $this->masked ?
Str::mask($this->value, '*', 0, intval(ceil(strlen($this->value) * 0.65))) : $this->value;
$printStr = $printStr->append("`{$value}`")->newLine();
$printStr = $printStr->append(\str_pad('environment_scope', $titlePad, pad_type: \STR_PAD_LEFT))
->append(' => ')->append('`')
->append($this->environment_scope->label())
->append('`')->newLine();
if ($this->protected or $this->masked or $this->raw) {
$printStr = $printStr->append(\str_pad('options', $titlePad, pad_type: \STR_PAD_LEFT))
->append(' => ')
->append(collect([
$this->protected ? 'protected' : null,
$this->masked ? 'masked' : null,
$this->raw ? 'raw' : null,
])->filter()->implode(','))->newLine();
}
$printStr = $printStr->append(\str_pad('variable_type', $titlePad, pad_type: \STR_PAD_LEFT))
->append(' => ')
->append("`{$this->variable_type}`");
// * BOTTOM ROW
$printStr = $printStr->newLine()
->append(\str_pad('', $rowLength, '_', \STR_PAD_LEFT))
->newLine();
return $printStr->toString();
}
}