Remove interact from command and implement bootstrap method

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos
2021-07-18 10:11:39 -03:00
committed by Julius Härtl
parent e01e4cf1a7
commit 6714c89220
7 changed files with 91 additions and 89 deletions

View File

@@ -72,20 +72,6 @@ class BoardImport extends Command {
;
}
/**
* @inheritDoc
*
* @return void
*/
protected function interact(InputInterface $input, OutputInterface $output) {
$this->boardImportCommandService
->setInput($input)
->setOutput($output)
->setSystem($input->getOption('system'))
->setConfigInstance($input->getOption('config'))
->validate();
}
/**
* @param InputInterface $input
* @param OutputInterface $output
@@ -97,6 +83,7 @@ class BoardImport extends Command {
->boardImportCommandService
->setInput($input)
->setOutput($output)
->setCommand($this)
->import();
$output->writeln('Done!');
return 0;

View File

@@ -57,7 +57,6 @@ class BoardImportApiController extends ApiController {
$config->owner = $this->userId;
$this->boardImportService->setConfigInstance($config);
$this->boardImportService->setData(json_decode(json_encode($data)));
$this->boardImportService->validate();
$this->boardImportService->import();
return new DataResponse($this->boardImportService->getBoard(), Http::STATUS_OK);
}

View File

@@ -63,7 +63,12 @@ abstract class ABoardImportService {
abstract public function assignCardsToLabels(): void;
abstract public function validateUsers(): void;
/**
* Configure import service
*
* @return void
*/
abstract public function bootstrap(): void;
public function setImportService(BoardImportService $service): void {
$this->boardImportService = $service;

View File

@@ -48,14 +48,9 @@ class BoardImportCommandService extends BoardImportService {
*/
private $output;
/**
* Define Command instance
*
* @param Command $command
* @return void
*/
public function setCommand(Command $command): void {
public function setCommand(Command $command): self {
$this->command = $command;
return $this;
}
public function getCommand(): Command {
@@ -80,13 +75,19 @@ class BoardImportCommandService extends BoardImportService {
return $this->output;
}
public function validate(): void {
$this->validateData();
parent::validate();
}
protected function validateConfig(): void {
try {
$config = $this->getInput()->getOption('config');
if (is_string($config)) {
if (!is_file($config)) {
throw new NotFoundException('Please inform a valid config json file');
}
$config = json_decode(file_get_contents($config));
if (!$config instanceof \stdClass) {
throw new NotFoundException('Please inform a valid config json file');
}
$this->setConfigInstance($config);
}
parent::validateConfig();
return;
} catch (NotFoundException $e) {
@@ -104,7 +105,7 @@ class BoardImportCommandService extends BoardImportService {
return $answer;
});
$configFile = $helper->ask($this->getInput(), $this->getOutput(), $question);
$this->setConfigInstance($configFile);
$config = $this->getInput()->setOption('config', $configFile);
} catch (ConflictException $e) {
$this->getOutput()->writeln('<error>Invalid config file</error>');
$this->getOutput()->writeln(array_map(function (array $v): string {
@@ -114,7 +115,6 @@ class BoardImportCommandService extends BoardImportService {
$schemaPath = __DIR__ . '/fixtures/config-' . $this->getSystem() . '-schema.json';
$this->getOutput()->writeln(print_r(file_get_contents($schemaPath), true));
$this->getInput()->setOption('config', null);
$this->setConfigInstance('');
}
parent::validateConfig();
return;
@@ -139,34 +139,41 @@ class BoardImportCommandService extends BoardImportService {
return;
}
private function validateData(): self {
$filename = $this->getInput()->getOption('data');
if (!is_string($filename) || empty($filename) || !is_file($filename)) {
$helper = $this->getCommand()->getHelper('question');
$question = new Question(
'Please inform a valid data json file: ',
'data.json'
);
$question->setValidator(function (string $answer) {
if (!is_file($answer)) {
throw new \RuntimeException(
'Data file not found'
);
}
return $answer;
});
$data = $helper->ask($this->getInput(), $this->getOutput(), $question);
$this->getInput()->setOption('data', $data);
protected function validateData(): void {
$data = $this->getInput()->getOption('data');
if (is_string($data)) {
$data = json_decode(file_get_contents($data));
if ($data instanceof \stdClass) {
$this->setData($data);
return;
}
}
$this->setData(json_decode(file_get_contents($filename)));
if (!$this->getData()) {
$this->getOutput()->writeln('<error>Is not a json file: ' . $filename . '</error>');
$this->validateData();
}
return $this;
$helper = $this->getCommand()->getHelper('question');
$question = new Question(
'Please inform a valid data json file: ',
'data.json'
);
$question->setValidator(function (string $answer) {
if (!is_file($answer)) {
throw new \RuntimeException(
'Data file not found'
);
}
return $answer;
});
$data = $helper->ask($this->getInput(), $this->getOutput(), $question);
$this->getInput()->setOption('data', $data);
$this->validateData();
}
public function bootstrap(): void {
$this->setSystem($this->getInput()->getOption('system'));
parent::bootstrap();
}
public function import(): void {
$this->getOutput()->writeln('Starting import...');
$this->bootstrap();
$this->getOutput()->writeln('Importing board...');
$this->importBoard();
$this->getOutput()->writeln('Assign users to board...');

View File

@@ -111,10 +111,10 @@ class BoardImportService {
$this->cardMapper = $cardMapper;
$this->assignmentMapper = $assignmentMapper;
$this->commentsManager = $commentsManager;
$this->setData(new \stdClass());
}
public function import(): void {
$this->bootstrap();
try {
$this->importBoard();
$this->importAcl();
@@ -129,12 +129,6 @@ class BoardImportService {
}
}
public function validate(): void {
$this->validateSystem();
$this->validateConfig();
$this->validateUsers();
}
public function validateSystem(): void {
if (!in_array($this->getSystem(), $this->getAllowedImportSystems())) {
throw new NotFoundException('Invalid system');
@@ -327,7 +321,7 @@ class BoardImportService {
$this->getImportSystem()->importParticipants();
}
final public function setData(\stdClass $data): void {
public function setData(\stdClass $data): void {
$this->data = $data;
}
@@ -355,7 +349,7 @@ class BoardImportService {
* @param string $configName config name
* @return mixed
*/
public function getConfig(string $configName = null) {
public function getConfig(string $configName) {
if (!property_exists($this->config, $configName)) {
return;
}
@@ -363,19 +357,10 @@ class BoardImportService {
}
/**
* @param mixed $config
* @param \stdClass $config
* @return self
*/
public function setConfigInstance($config): self {
if (is_string($config)) {
if (!is_file($config)) {
throw new NotFoundException('Please inform a valid config json file');
}
$config = json_decode(file_get_contents($config));
if (!is_object($config)) {
throw new NotFoundException('Please inform a valid config json file');
}
}
$this->config = $config;
return $this;
}
@@ -413,7 +398,13 @@ class BoardImportService {
$this->setConfig('owner', $owner);
}
public function validateUsers(): void {
$this->getImportSystem()->validateUsers();
protected function validateData(): void {
}
public function bootstrap(): void {
$this->validateSystem();
$this->validateConfig();
$this->validateData();
$this->getImportSystem()->bootstrap();
}
}

View File

@@ -66,6 +66,10 @@ class BoardImportTrelloService extends ABoardImportService {
$this->l10n = $l10n;
}
public function bootstrap(): void {
$this->validateUsers();
}
public function validateUsers(): void {
if (empty($this->getImportService()->getConfig('uidRelation'))) {
return;

View File

@@ -76,6 +76,22 @@ class BoardImportServiceTest extends \Test\TestCase {
$this->assignmentMapper,
$this->commentsManager
);
$this->boardImportService->setSystem('trello');
$data = json_decode(file_get_contents(__DIR__ . '/../../data/data-trello.json'));
$this->boardImportService->setData($data);
$configInstance = json_decode(file_get_contents(__DIR__ . '/../../data/config-trello.json'));
$this->boardImportService->setConfigInstance($configInstance);
$owner = $this->createMock(IUser::class);
$owner
->method('getUID')
->willReturn('admin');
$this->userManager
->method('get')
->willReturn($owner);
}
public function testImportSuccess() {
@@ -91,25 +107,18 @@ class BoardImportServiceTest extends \Test\TestCase {
}
public function testImportBoard() {
$this->boardImportService->setSystem('trello');
$data = json_decode(file_get_contents(__DIR__ . '/../../data/data-trello.json'));
$this->boardImportService->setData($data);
$configInstance = json_decode(file_get_contents(__DIR__ . '/../../data/config-trello.json'));
$this->boardImportService->setConfigInstance($configInstance);
$owner = $this->createMock(IUser::class);
$owner
->method('getUID')
->willReturn('owner');
$this->userManager
->method('get')
->willReturn($owner);
$this->boardImportService->validateOwner();
$actual = $this->boardImportService->importBoard();
$this->assertNull($actual);
$board = $this->boardImportService->getBoard();
$this->assertEquals('Test Board Name', $board->getTitle());
$this->assertEquals('owner', $board->getOwner());
$this->assertEquals('admin', $board->getOwner());
$this->assertEquals('0800fd', $board->getColor());
}
public function testImportAcl() {
$this->markTestIncomplete();
$actual = $this->boardImportService->importAcl();
$this->assertNull($actual);
}
}