Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 6, 2024
1 parent fe18aa5 commit 3a77aa3
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/Task/RealMeSetupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Exception;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\RealMe\RealMeService;
use SilverStripe\Control\Director;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\BuildTask;
use SilverStripe\HybridExecution\HybridOutput;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
* Class RealMeSetupTask
Expand All @@ -25,15 +27,16 @@
*/
class RealMeSetupTask extends BuildTask
{
private static $segment = 'RealMeSetupTask';
protected static string $commandName = 'RealMeSetupTask';

private static $dependencies = [
'Service' => '%$' . RealMeService::class,
];

protected $title = "RealMe Setup Task";
protected string $title = "RealMe Setup Task";

protected $description = 'Validates a realme configuration & creates the resources needed to integrate with realme';
protected static string $description = 'Validates a realme configuration & creates the resources '
. 'needed to integrate with realme';

/**
* @var RealMeService
Expand All @@ -47,13 +50,14 @@ class RealMeSetupTask extends BuildTask
*/
private $errors = array();

private HybridOutput $output;

/**
* Run this setup task. See class phpdoc for the full description of what this does
*
* @param HTTPRequest $request
*/
public function run($request)
protected function execute(InputInterface $input, HybridOutput $output): int
{
$this->output = $output;
try {
// Ensure we are running on the command-line, and not running in a browser
if (false === Director::is_cli()) {
Expand All @@ -64,22 +68,37 @@ public function run($request)
}

// Validate all required values exist
$forEnv = $request->getVar('forEnv');
$forEnv = $input->getOption('forEnv');

// Throws an exception if there was a problem with the config.
$this->validateInputs($forEnv);

$this->outputMetadataXmlContent($forEnv);

$this->message(PHP_EOL . _t(
$this->output->writeln(['', _t(
RealMeSetupTask::class . '.BUILD_FINISH',
'RealMe setup complete. Please copy the XML into a file for upload to the {env} environment or DIA ' .
'to complete the integration',
array('env' => $forEnv)
));
)]);
} catch (Exception $e) {
$this->message($e->getMessage() . PHP_EOL);
$this->output->writeln('<error>' . $e->getMessage() . '</>');
return Command::FAILURE;
}
return Command::SUCCESS;
}

public function getOptions(): array
{
return [
new InputOption(
'forEnv',
null,
InputOption::VALUE_REQUIRED,
'The RealMe environment to set up',
suggestedValues: $this->service->getAllowedRealMeEnvironments()
),
];
}

/**
Expand Down Expand Up @@ -135,7 +154,7 @@ private function validateInputs($forEnv)
));
}

$this->message(_t(
$this->output->writeln(_t(
RealMeSetupTask::class . '.VALIDATION_SUCCESS',
'Validation succeeded, continuing with setup...'
));
Expand All @@ -149,7 +168,7 @@ private function validateInputs($forEnv)
private function outputMetadataXmlContent($forEnv)
{
// Output metadata XML so that it can be sent to RealMe via the agency
$this->message(_t(
$this->output->writeln(_t(
RealMeSetupTask::class . '.OUPUT_PREFIX',
'Metadata XML is listed below for the \'{env}\' RealMe environment, this should be sent to the agency so ' .
'they can pass it on to RealMe Operations staff',
Expand Down Expand Up @@ -181,7 +200,7 @@ private function outputMetadataXmlContent($forEnv)
)
);

$this->message($message);
$this->output->writeln($message);
}

/**
Expand Down Expand Up @@ -223,16 +242,6 @@ private function getConfigurationTemplateDir()
return $path . '/templates/saml-conf';
}

/**
* Output a message to the console
* @param string $message
* @return void
*/
private function message($message)
{
echo $message . PHP_EOL;
}

/**
* Thin wrapper around is_readable(), used mainly so we can test this class completely
*
Expand Down

0 comments on commit 3a77aa3

Please sign in to comment.