Refactor and improvements on command
Check available helpers Default permission: view only Moved validate setting from helper to command Turn more easy create a importer Docblock and improvements on interface lcfirst on system property Helper moved to ImporHelper folder Moved fixtures to ImportHelper Rename settings to config Big refactor to move import methods to service Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
committed by
Julius Härtl
parent
eb8bf3f22b
commit
fd92fc3c4d
@@ -23,26 +23,23 @@
|
||||
|
||||
namespace OCA\Deck\Command;
|
||||
|
||||
use OCA\Deck\Command\Helper\ImportInterface;
|
||||
use OCA\Deck\Command\Helper\TrelloHelper;
|
||||
use JsonSchema\Constraints\Constraint;
|
||||
use JsonSchema\Validator;
|
||||
use OCA\Deck\Command\ImportHelper\AImport;
|
||||
use OCA\Deck\Command\ImportHelper\TrelloHelper;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class BoardImport extends Command {
|
||||
/** @var string */
|
||||
private $system;
|
||||
private $allowedSystems = ['trello'];
|
||||
private $allowedSystems;
|
||||
/** @var TrelloHelper */
|
||||
private $trelloHelper;
|
||||
/**
|
||||
* Data object created from settings JSON
|
||||
*
|
||||
* @var \StdClass
|
||||
*/
|
||||
public $settings;
|
||||
|
||||
public function __construct(
|
||||
TrelloHelper $trelloHelper
|
||||
@@ -55,6 +52,11 @@ class BoardImport extends Command {
|
||||
* @return void
|
||||
*/
|
||||
protected function configure() {
|
||||
$allowedSystems = glob(__DIR__ . '/ImportHelper/*Helper.php');
|
||||
$this->allowedSystems = array_map(function ($name) {
|
||||
preg_match('/\/(?<system>\w+)Helper\.php$/', $name, $matches);
|
||||
return lcfirst($matches['system']);
|
||||
}, $allowedSystems);
|
||||
$this
|
||||
->setName('deck:import')
|
||||
->setDescription('Import data')
|
||||
@@ -62,11 +64,11 @@ class BoardImport extends Command {
|
||||
'system',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Source system for import. Available options: trello.',
|
||||
'Source system for import. Available options: ' . implode(', ', $this->allowedSystems) . '.',
|
||||
'trello'
|
||||
)
|
||||
->addOption(
|
||||
'setting',
|
||||
'config',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Configuration json file.',
|
||||
@@ -89,18 +91,64 @@ class BoardImport extends Command {
|
||||
*/
|
||||
protected function interact(InputInterface $input, OutputInterface $output) {
|
||||
$this->validateSystem($input, $output);
|
||||
$this->getSystem()
|
||||
$this->validateConfig($input, $output);
|
||||
$this->getSystemHelper()
|
||||
->validate($input, $output);
|
||||
}
|
||||
|
||||
protected function validateConfig(InputInterface $input, OutputInterface $output): void {
|
||||
$configFile = $input->getOption('config');
|
||||
if (!is_file($configFile)) {
|
||||
$helper = $this->getHelper('question');
|
||||
$question = new Question(
|
||||
'Please inform a valid config json file: ',
|
||||
'config.json'
|
||||
);
|
||||
$question->setValidator(function ($answer) {
|
||||
if (!is_file($answer)) {
|
||||
throw new \RuntimeException(
|
||||
'config file not found'
|
||||
);
|
||||
}
|
||||
return $answer;
|
||||
});
|
||||
$configFile = $helper->ask($input, $output, $question);
|
||||
$input->setOption('config', $configFile);
|
||||
}
|
||||
|
||||
$config = json_decode(file_get_contents($configFile));
|
||||
$schemaPath = __DIR__ . '/ImportHelper/fixtures/config-' . $this->getSystem() . '-schema.json';
|
||||
$validator = new Validator();
|
||||
$validator->validate(
|
||||
$config,
|
||||
(object)['$ref' => 'file://' . realpath($schemaPath)],
|
||||
Constraint::CHECK_MODE_APPLY_DEFAULTS
|
||||
);
|
||||
if (!$validator->isValid()) {
|
||||
$output->writeln('<error>Invalid config file</error>');
|
||||
$output->writeln(array_map(function ($v) {
|
||||
return $v['message'];
|
||||
}, $validator->getErrors()));
|
||||
$output->writeln('Valid schema:');
|
||||
$output->writeln(print_r(file_get_contents($schemaPath), true));
|
||||
$input->setOption('config', null);
|
||||
$this->validateConfig($input, $output);
|
||||
}
|
||||
$this->getSystemHelper()->setConfigInstance($config);
|
||||
}
|
||||
|
||||
private function setSystem(string $system): void {
|
||||
$this->system = $system;
|
||||
}
|
||||
|
||||
public function getSystem() {
|
||||
return $this->system;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ImportInterface
|
||||
* @return AImport
|
||||
*/
|
||||
private function getSystem() {
|
||||
private function getSystemHelper() {
|
||||
$helper = $this->{$this->system . 'Helper'};
|
||||
$helper->setCommand($this);
|
||||
return $helper;
|
||||
@@ -134,7 +182,7 @@ class BoardImport extends Command {
|
||||
* @return int
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$this->getSystem()
|
||||
$this->getSystemHelper()
|
||||
->import($input, $output);
|
||||
$output->writeln('Done!');
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user