Skip to content

Commit 76ce4a8

Browse files
committed
DEVSUPP-160: Moved common code to abstract class
1 parent 8cbf034 commit 76ce4a8

3 files changed

Lines changed: 33 additions & 36 deletions

File tree

src/Command/AbstractImportCommand.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,46 @@
33
namespace App\Command;
44

55
use App\Entity\ImportRun;
6+
use App\Service\BaseImporter;
67
use Doctrine\ORM\EntityManagerInterface;
78
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Output\OutputInterface;
810

911
abstract class AbstractImportCommand extends Command
1012
{
1113
protected $entityManager;
14+
protected $importer;
1215

13-
public function __construct(EntityManagerInterface $entityManager)
16+
public function __construct(BaseImporter $importer, EntityManagerInterface $entityManager)
1417
{
1518
parent::__construct();
1619

1720
$this->entityManager = $entityManager;
21+
$this->importer = $importer;
22+
}
23+
24+
/**
25+
* Run the importer.
26+
*
27+
* @param string $type
28+
* @param string $src
29+
* @param \Symfony\Component\Console\Output\OutputInterface $output
30+
* @throws \Exception
31+
*/
32+
protected function import(string $type, string $src, OutputInterface $output)
33+
{
34+
$success = true;
35+
$errorMessage = null;
36+
37+
try {
38+
$this->importer->import($src);
39+
} catch (\Exception $e) {
40+
$success = false;
41+
$errorMessage = $e->getMessage();
42+
$output->writeln($errorMessage);
43+
}
44+
45+
$this->recordImportRun($type, $success, $errorMessage);
1846
}
1947

2048
/**

src/Command/ReportImportCommand.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111

1212
class ReportImportCommand extends AbstractImportCommand
1313
{
14-
private $reportImporter;
15-
1614
public function __construct(ReportImporter $reportImporter, EntityManagerInterface $entityManager)
1715
{
18-
parent::__construct($entityManager);
19-
20-
$this->reportImporter = $reportImporter;
21-
$this->entityManager = $entityManager;
16+
parent::__construct($reportImporter, $entityManager);
2217
}
2318

2419
protected function configure()
@@ -31,17 +26,6 @@ protected function configure()
3126

3227
protected function execute(InputInterface $input, OutputInterface $output)
3328
{
34-
$success = true;
35-
$errorMessage = null;
36-
37-
try {
38-
$this->reportImporter->import($input->getArgument('src'));
39-
} catch (\Exception $e) {
40-
$success = false;
41-
$errorMessage = $e->getMessage();
42-
$output->writeln($errorMessage);
43-
}
44-
45-
$this->recordImportRun(Report::class, $success, $errorMessage);
29+
$this->import(Report::class, $input->getArgument('src'), $output);
4630
}
4731
}

src/Command/SystemImportCommand.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111

1212
class SystemImportCommand extends AbstractImportCommand
1313
{
14-
private $systemImporter;
15-
1614
public function __construct(SystemImporter $systemImporter, EntityManagerInterface $entityManager)
1715
{
18-
parent::__construct($entityManager);
19-
20-
$this->systemImporter = $systemImporter;
16+
parent::__construct($systemImporter, $entityManager);
2117
}
2218

2319
protected function configure()
@@ -30,17 +26,6 @@ protected function configure()
3026

3127
protected function execute(InputInterface $input, OutputInterface $output)
3228
{
33-
$success = true;
34-
$errorMessage = null;
35-
36-
try {
37-
$this->systemImporter->import($input->getArgument('src'));
38-
} catch (\Exception $e) {
39-
$success = false;
40-
$errorMessage = $e->getMessage();
41-
$output->writeln($errorMessage);
42-
}
43-
44-
$this->recordImportRun(System::class, $success, $errorMessage);
29+
$this->import(System::class, $input->getArgument('src'), $output);
4530
}
4631
}

0 commit comments

Comments
 (0)