Skip to content

Commit

Permalink
chore: Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Huber committed Nov 18, 2024
1 parent 43ab8e9 commit ea840f3
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 68 deletions.
62 changes: 36 additions & 26 deletions src/Method/ScottyCtlOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,74 @@

class ScottyCtlOptions
{

protected array $data;


public function __construct(
protected readonly HostConfig $hostConfig,
protected readonly TaskContextInterface $context
) {
$this->data = [];
$scotty_data = $hostConfig->getData()->get('scotty');
$scotty_data = $hostConfig->getData()->get("scotty");
if (!$scotty_data) {
throw new InvalidArgumentException('Missing scotty configuration');
throw new InvalidArgumentException("Missing scotty configuration");
}

foreach (['server', 'access-token'] as $key) {
foreach (["server", "access-token"] as $key) {
if ($scotty_data->has($key)) {
$this->data[$key] = $scotty_data->get($key)->getValue();
}
}
if ($this->data['access-token']) {
$this->context->getPasswordManager()->registerCustomSecretToObfuscate($this->data['access-token']);
if ($this->data["access-token"]) {
$this->context
->getPasswordManager()
->registerCustomSecretToObfuscate($this->data["access-token"]);
}

$this->data['app-name'] = $scotty_data['app-name'] ?? '%host.configName%';
$this->data["app-name"] =
$scotty_data["app-name"] ?? "%host.configName%";
}

public function build($command, $additional_data = []): array
public function build(string $command, array $additional_data = []): array
{
$variables = Utilities::buildVariablesFrom($this->hostConfig, $this->context);
$variables = Utilities::buildVariablesFrom(
$this->hostConfig,
$this->context
);
$replacements = Utilities::expandVariables($variables);

$data = Utilities::expandStrings(array_merge($this->data, $additional_data), $replacements);
$data = Utilities::expandStrings(
array_merge($this->data, $additional_data),
$replacements
);

return $this->buildImpl($data, $command);
}

protected function buildImpl(array $data, string $command): array
{

$options = [
'--server',
$this->data['server'],
];
if ($data['access-token']) {
$options[] = '--access-token';
$options[] = $data['access-token'];
$options = ["--server", $this->data["server"]];
if ($data["access-token"]) {
$options[] = "--access-token";
$options[] = $data["access-token"];
}
$options[] = $command;
$options[] = $data['app-name'];
$options[] = $data["app-name"];

return $options;
}

public function runInShell(ShellProviderInterface $shell, string $command, array $add_data = []): CommandResult
{
return $shell->run(sprintf(
'#!scottyctl %s',
implode(' ', $this->build($command, $add_data))
), true, false);
public function runInShell(
ShellProviderInterface $shell,
string $command,
array $add_data = []
): CommandResult {
return $shell->run(
sprintf(
"#!scottyctl %s",
implode(" ", $this->build($command, $add_data))
),
true,
false
);
}
}
149 changes: 107 additions & 42 deletions src/Method/ScottyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,122 @@

class ScottyMethod extends BaseMethod
{

use ScaffoldHelperTrait;

public function getName(): string
{
return 'scotty';
return "scotty";
}

public function supports(string $method_name): bool
{
return $method_name === $this->getName();
}

public function getDefaultConfig(ConfigurationService $configuration_service, Node $host_config): Node
{
$parent = parent::getDefaultConfig($configuration_service, $host_config);
public function getDefaultConfig(
ConfigurationService $configuration_service,
Node $host_config
): Node {
$parent = parent::getDefaultConfig(
$configuration_service,
$host_config
);
$config = [
'scotty' => $configuration_service->getSetting('scotty', []),
"scotty" => $configuration_service->getSetting("scotty", []),
];
$config['executables']['scottyctl'] = 'scottyctl';
$config['scotty']['scaffold'] = $this->getScaffoldDefaultConfig($host_config, $config, 'scotty');

return $parent->merge(new Node($config, $this->getName() . ' method defaults'));
$config["executables"]["scottyctl"] = "scottyctl";
$config["scotty"]["scaffold"] = $this->getScaffoldDefaultConfig(
$host_config,
$config,
"scotty"
);

return $parent->merge(
new Node($config, $this->getName() . " method defaults")
);
}

public function validateConfig(ConfigurationService $configuration_service, Node $config, ValidationErrorBagInterface $errors): void
{
public function validateConfig(
ConfigurationService $configuration_service,
Node $config,
ValidationErrorBagInterface $errors
): void {
parent::validateConfig($configuration_service, $config, $errors); // TODO: Change the autogenerated stub
$this->validateScaffoldConfig($config, 'scotty', $errors);
$validation_service = new ValidationService($config->get('scotty'), $errors, 'host.' . $config['configName']. '.scotty');
$this->validateScaffoldConfig($config, "scotty", $errors);
$validation_service = new ValidationService(
$config->get("scotty"),
$errors,
"host." . $config["configName"] . ".scotty"
);

$validation_service->isArray(
"scaffold",
"The scaffold configuration needs to be an array"
);
$validation_service->hasKey(
"server",
"The scotty configuration needs a `server` key"
);
}

$validation_service->isArray('scaffold', 'The scaffold configuration needs to be an array');
$validation_service->hasKey('server', 'The scotty configuration needs a `server` key');
public function alterConfig(
ConfigurationService $configuration_service,
Node $data
): void {
parent::alterConfig($configuration_service, $data);

$replacements = Utilities::expandVariables([
"globals" => Utilities::getGlobalReplacements(
$configuration_service
),
"settings" => $configuration_service->getAllSettings(),
"host" => $data->asArray(),
]);

$data->expandReplacements($replacements, []);
}

public function scaffoldApp(HostConfig $host_config, TaskContext $context): string
{
$project_folder = $context->get('installDir', Utilities::getTempFolder($host_config, 'scotty'));
public function scaffoldApp(
HostConfig $host_config,
TaskContext $context
): string {
$project_folder = $context->get(
"installDir",
Utilities::getTempFolder($host_config, "scotty")
);
$shell = $host_config->shell();
$this->runScaffolder($host_config, $context, $shell, $project_folder, 'scotty');
$this->runScaffolder(
$host_config,
$context,
$shell,
$project_folder,
"scotty"
);
return $project_folder;
}

protected function destroyApp(HostConfig $host_config, TaskContext $context): void
{
protected function destroyApp(
HostConfig $host_config,
TaskContext $context
): void {
$options = new ScottyCtlOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), 'destroy');
$result = $options->runInShell($host_config->shell(), "destroy");
if ($result->failed()) {
$result->throwException('Failed to run scottyctl destroy');
$result->throwException("Failed to run scottyctl destroy");
}
}

protected function createApp(HostConfig $host_config, TaskContext $context): void
{
protected function createApp(
HostConfig $host_config,
TaskContext $context
): void {
$app_folder = $this->scaffoldApp($host_config, $context);
$options = new ScottyCtlCreateOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), 'create', ['app_folder' => $app_folder]);
$result = $options->runInShell($host_config->shell(), "create", [
"app_folder" => $app_folder,
]);
if ($result->failed()) {
$result->throwException('Failed to run scottyctl create');
$result->throwException("Failed to run scottyctl create");
}
}

Expand All @@ -78,32 +135,40 @@ public function deploy(HostConfig $host_config, TaskContext $context): void
$this->createApp($host_config, $context);
}

public function appCheckExisting(HostConfig $host_config, TaskContext $context): bool
{
public function appCheckExisting(
HostConfig $host_config,
TaskContext $context
): bool {
$options = new ScottyCtlOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), 'info')->succeeded();
$context->setResult('appExists', $result);
$result = $options
->runInShell($host_config->shell(), "info")
->succeeded();
$context->setResult("appExists", $result);
return $result;
}

public function appCreate(HostConfig $host_config, TaskContext $context): void
{
$stage = $context->get('currentStage');
public function appCreate(
HostConfig $host_config,
TaskContext $context
): void {
$stage = $context->get("currentStage");
if (!$stage) {
throw new \InvalidArgumentException('Missing current stage');
throw new \InvalidArgumentException("Missing current stage");
}
if ($stage === 'installCode') {
if ($stage === "installCode") {
$this->createApp($host_config, $context);
}
}

public function appDestroy(HostConfig $host_config, TaskContext $context): void
{
$stage = $context->get('currentStage');
public function appDestroy(
HostConfig $host_config,
TaskContext $context
): void {
$stage = $context->get("currentStage");
if (!$stage) {
throw new \InvalidArgumentException('Missing current stage');
throw new \InvalidArgumentException("Missing current stage");
}
if ($stage === 'deleteCode') {
if ($stage === "deleteCode") {
$this->destroyApp($host_config, $context);
}
}
Expand Down

0 comments on commit ea840f3

Please sign in to comment.