Remove interact from command and implement bootstrap method
Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
committed by
Julius Härtl
parent
e01e4cf1a7
commit
6714c89220
@@ -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 InputInterface $input
|
||||||
* @param OutputInterface $output
|
* @param OutputInterface $output
|
||||||
@@ -97,6 +83,7 @@ class BoardImport extends Command {
|
|||||||
->boardImportCommandService
|
->boardImportCommandService
|
||||||
->setInput($input)
|
->setInput($input)
|
||||||
->setOutput($output)
|
->setOutput($output)
|
||||||
|
->setCommand($this)
|
||||||
->import();
|
->import();
|
||||||
$output->writeln('Done!');
|
$output->writeln('Done!');
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -57,7 +57,6 @@ class BoardImportApiController extends ApiController {
|
|||||||
$config->owner = $this->userId;
|
$config->owner = $this->userId;
|
||||||
$this->boardImportService->setConfigInstance($config);
|
$this->boardImportService->setConfigInstance($config);
|
||||||
$this->boardImportService->setData(json_decode(json_encode($data)));
|
$this->boardImportService->setData(json_decode(json_encode($data)));
|
||||||
$this->boardImportService->validate();
|
|
||||||
$this->boardImportService->import();
|
$this->boardImportService->import();
|
||||||
return new DataResponse($this->boardImportService->getBoard(), Http::STATUS_OK);
|
return new DataResponse($this->boardImportService->getBoard(), Http::STATUS_OK);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,12 @@ abstract class ABoardImportService {
|
|||||||
|
|
||||||
abstract public function assignCardsToLabels(): void;
|
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 {
|
public function setImportService(BoardImportService $service): void {
|
||||||
$this->boardImportService = $service;
|
$this->boardImportService = $service;
|
||||||
|
|||||||
@@ -48,14 +48,9 @@ class BoardImportCommandService extends BoardImportService {
|
|||||||
*/
|
*/
|
||||||
private $output;
|
private $output;
|
||||||
|
|
||||||
/**
|
public function setCommand(Command $command): self {
|
||||||
* Define Command instance
|
|
||||||
*
|
|
||||||
* @param Command $command
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setCommand(Command $command): void {
|
|
||||||
$this->command = $command;
|
$this->command = $command;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCommand(): Command {
|
public function getCommand(): Command {
|
||||||
@@ -80,13 +75,19 @@ class BoardImportCommandService extends BoardImportService {
|
|||||||
return $this->output;
|
return $this->output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validate(): void {
|
|
||||||
$this->validateData();
|
|
||||||
parent::validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function validateConfig(): void {
|
protected function validateConfig(): void {
|
||||||
try {
|
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();
|
parent::validateConfig();
|
||||||
return;
|
return;
|
||||||
} catch (NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
@@ -104,7 +105,7 @@ class BoardImportCommandService extends BoardImportService {
|
|||||||
return $answer;
|
return $answer;
|
||||||
});
|
});
|
||||||
$configFile = $helper->ask($this->getInput(), $this->getOutput(), $question);
|
$configFile = $helper->ask($this->getInput(), $this->getOutput(), $question);
|
||||||
$this->setConfigInstance($configFile);
|
$config = $this->getInput()->setOption('config', $configFile);
|
||||||
} catch (ConflictException $e) {
|
} catch (ConflictException $e) {
|
||||||
$this->getOutput()->writeln('<error>Invalid config file</error>');
|
$this->getOutput()->writeln('<error>Invalid config file</error>');
|
||||||
$this->getOutput()->writeln(array_map(function (array $v): string {
|
$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';
|
$schemaPath = __DIR__ . '/fixtures/config-' . $this->getSystem() . '-schema.json';
|
||||||
$this->getOutput()->writeln(print_r(file_get_contents($schemaPath), true));
|
$this->getOutput()->writeln(print_r(file_get_contents($schemaPath), true));
|
||||||
$this->getInput()->setOption('config', null);
|
$this->getInput()->setOption('config', null);
|
||||||
$this->setConfigInstance('');
|
|
||||||
}
|
}
|
||||||
parent::validateConfig();
|
parent::validateConfig();
|
||||||
return;
|
return;
|
||||||
@@ -139,34 +139,41 @@ class BoardImportCommandService extends BoardImportService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateData(): self {
|
protected function validateData(): void {
|
||||||
$filename = $this->getInput()->getOption('data');
|
$data = $this->getInput()->getOption('data');
|
||||||
if (!is_string($filename) || empty($filename) || !is_file($filename)) {
|
if (is_string($data)) {
|
||||||
$helper = $this->getCommand()->getHelper('question');
|
$data = json_decode(file_get_contents($data));
|
||||||
$question = new Question(
|
if ($data instanceof \stdClass) {
|
||||||
'Please inform a valid data json file: ',
|
$this->setData($data);
|
||||||
'data.json'
|
return;
|
||||||
);
|
}
|
||||||
$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->setData(json_decode(file_get_contents($filename)));
|
$helper = $this->getCommand()->getHelper('question');
|
||||||
if (!$this->getData()) {
|
$question = new Question(
|
||||||
$this->getOutput()->writeln('<error>Is not a json file: ' . $filename . '</error>');
|
'Please inform a valid data json file: ',
|
||||||
$this->validateData();
|
'data.json'
|
||||||
}
|
);
|
||||||
return $this;
|
$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 {
|
public function import(): void {
|
||||||
|
$this->getOutput()->writeln('Starting import...');
|
||||||
|
$this->bootstrap();
|
||||||
$this->getOutput()->writeln('Importing board...');
|
$this->getOutput()->writeln('Importing board...');
|
||||||
$this->importBoard();
|
$this->importBoard();
|
||||||
$this->getOutput()->writeln('Assign users to board...');
|
$this->getOutput()->writeln('Assign users to board...');
|
||||||
|
|||||||
@@ -111,10 +111,10 @@ class BoardImportService {
|
|||||||
$this->cardMapper = $cardMapper;
|
$this->cardMapper = $cardMapper;
|
||||||
$this->assignmentMapper = $assignmentMapper;
|
$this->assignmentMapper = $assignmentMapper;
|
||||||
$this->commentsManager = $commentsManager;
|
$this->commentsManager = $commentsManager;
|
||||||
$this->setData(new \stdClass());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function import(): void {
|
public function import(): void {
|
||||||
|
$this->bootstrap();
|
||||||
try {
|
try {
|
||||||
$this->importBoard();
|
$this->importBoard();
|
||||||
$this->importAcl();
|
$this->importAcl();
|
||||||
@@ -129,12 +129,6 @@ class BoardImportService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validate(): void {
|
|
||||||
$this->validateSystem();
|
|
||||||
$this->validateConfig();
|
|
||||||
$this->validateUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function validateSystem(): void {
|
public function validateSystem(): void {
|
||||||
if (!in_array($this->getSystem(), $this->getAllowedImportSystems())) {
|
if (!in_array($this->getSystem(), $this->getAllowedImportSystems())) {
|
||||||
throw new NotFoundException('Invalid system');
|
throw new NotFoundException('Invalid system');
|
||||||
@@ -327,7 +321,7 @@ class BoardImportService {
|
|||||||
$this->getImportSystem()->importParticipants();
|
$this->getImportSystem()->importParticipants();
|
||||||
}
|
}
|
||||||
|
|
||||||
final public function setData(\stdClass $data): void {
|
public function setData(\stdClass $data): void {
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +349,7 @@ class BoardImportService {
|
|||||||
* @param string $configName config name
|
* @param string $configName config name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getConfig(string $configName = null) {
|
public function getConfig(string $configName) {
|
||||||
if (!property_exists($this->config, $configName)) {
|
if (!property_exists($this->config, $configName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -363,19 +357,10 @@ class BoardImportService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $config
|
* @param \stdClass $config
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function setConfigInstance($config): 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;
|
$this->config = $config;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -413,7 +398,13 @@ class BoardImportService {
|
|||||||
$this->setConfig('owner', $owner);
|
$this->setConfig('owner', $owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validateUsers(): void {
|
protected function validateData(): void {
|
||||||
$this->getImportSystem()->validateUsers();
|
}
|
||||||
|
|
||||||
|
public function bootstrap(): void {
|
||||||
|
$this->validateSystem();
|
||||||
|
$this->validateConfig();
|
||||||
|
$this->validateData();
|
||||||
|
$this->getImportSystem()->bootstrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,10 @@ class BoardImportTrelloService extends ABoardImportService {
|
|||||||
$this->l10n = $l10n;
|
$this->l10n = $l10n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function bootstrap(): void {
|
||||||
|
$this->validateUsers();
|
||||||
|
}
|
||||||
|
|
||||||
public function validateUsers(): void {
|
public function validateUsers(): void {
|
||||||
if (empty($this->getImportService()->getConfig('uidRelation'))) {
|
if (empty($this->getImportService()->getConfig('uidRelation'))) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -76,6 +76,22 @@ class BoardImportServiceTest extends \Test\TestCase {
|
|||||||
$this->assignmentMapper,
|
$this->assignmentMapper,
|
||||||
$this->commentsManager
|
$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() {
|
public function testImportSuccess() {
|
||||||
@@ -91,25 +107,18 @@ class BoardImportServiceTest extends \Test\TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testImportBoard() {
|
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();
|
$this->boardImportService->validateOwner();
|
||||||
$actual = $this->boardImportService->importBoard();
|
$actual = $this->boardImportService->importBoard();
|
||||||
$this->assertNull($actual);
|
$this->assertNull($actual);
|
||||||
$board = $this->boardImportService->getBoard();
|
$board = $this->boardImportService->getBoard();
|
||||||
$this->assertEquals('Test Board Name', $board->getTitle());
|
$this->assertEquals('Test Board Name', $board->getTitle());
|
||||||
$this->assertEquals('owner', $board->getOwner());
|
$this->assertEquals('admin', $board->getOwner());
|
||||||
$this->assertEquals('0800fd', $board->getColor());
|
$this->assertEquals('0800fd', $board->getColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testImportAcl() {
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
$actual = $this->boardImportService->importAcl();
|
||||||
|
$this->assertNull($actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user