Skip to content

Commit 8cbf034

Browse files
committed
DEVSUPP-160: Fixed issue raised in code review
1 parent 4b2ede5 commit 8cbf034

3 files changed

Lines changed: 47 additions & 27 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use App\Entity\ImportRun;
6+
use Doctrine\ORM\EntityManagerInterface;
7+
use Symfony\Component\Console\Command\Command;
8+
9+
abstract class AbstractImportCommand extends Command
10+
{
11+
protected $entityManager;
12+
13+
public function __construct(EntityManagerInterface $entityManager)
14+
{
15+
parent::__construct();
16+
17+
$this->entityManager = $entityManager;
18+
}
19+
20+
/**
21+
* Record import run.
22+
*
23+
* @param string $type
24+
* The type of the import
25+
* @param bool $success
26+
* Success of run
27+
* @param string|null $output
28+
* Output message or null
29+
* @throws \Exception
30+
*/
31+
protected function recordImportRun(string $type, bool $success, string $output = null) {
32+
$importRun = new ImportRun();
33+
$importRun->setDatetime(new \DateTime());
34+
$importRun->setOutput($output);
35+
$importRun->setResult($success);
36+
$importRun->setType($type);
37+
38+
$this->entityManager->persist($importRun);
39+
$this->entityManager->flush();
40+
}
41+
}

src/Command/ReportImportCommand.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@
22

33
namespace App\Command;
44

5-
use App\Entity\ImportRun;
65
use App\Entity\Report;
76
use App\Service\ReportImporter;
87
use Doctrine\ORM\EntityManagerInterface;
9-
use Symfony\Component\Console\Command\Command;
108
use Symfony\Component\Console\Input\InputInterface;
119
use Symfony\Component\Console\Output\OutputInterface;
1210
use Symfony\Component\Console\Input\InputArgument;
1311

14-
class ReportImportCommand extends Command
12+
class ReportImportCommand extends AbstractImportCommand
1513
{
1614
private $reportImporter;
17-
private $entityManager;
1815

1916
public function __construct(ReportImporter $reportImporter, EntityManagerInterface $entityManager)
2017
{
21-
parent::__construct();
18+
parent::__construct($entityManager);
2219

2320
$this->reportImporter = $reportImporter;
2421
$this->entityManager = $entityManager;
@@ -45,13 +42,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
4542
$output->writeln($errorMessage);
4643
}
4744

48-
$importRun = new ImportRun();
49-
$importRun->setDatetime(new \DateTime());
50-
$importRun->setOutput($success ? 'OK' : $errorMessage);
51-
$importRun->setResult($success);
52-
$importRun->setType(Report::class);
53-
54-
$this->entityManager->persist($importRun);
55-
$this->entityManager->flush();
45+
$this->recordImportRun(Report::class, $success, $errorMessage);
5646
}
5747
}

src/Command/SystemImportCommand.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22

33
namespace App\Command;
44

5-
use App\Entity\ImportRun;
65
use App\Entity\System;
76
use App\Service\SystemImporter;
87
use Doctrine\ORM\EntityManagerInterface;
9-
use Symfony\Component\Console\Command\Command;
108
use Symfony\Component\Console\Input\InputInterface;
119
use Symfony\Component\Console\Output\OutputInterface;
1210
use Symfony\Component\Console\Input\InputArgument;
1311

14-
class SystemImportCommand extends Command
12+
class SystemImportCommand extends AbstractImportCommand
1513
{
1614
private $systemImporter;
17-
private $entityManager;
1815

1916
public function __construct(SystemImporter $systemImporter, EntityManagerInterface $entityManager)
2017
{
21-
parent::__construct();
18+
parent::__construct($entityManager);
2219

2320
$this->systemImporter = $systemImporter;
24-
$this->entityManager = $entityManager;
2521
}
2622

2723
protected function configure()
@@ -45,13 +41,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
4541
$output->writeln($errorMessage);
4642
}
4743

48-
$importRun = new ImportRun();
49-
$importRun->setDatetime(new \DateTime());
50-
$importRun->setOutput($success ? 'OK' : $errorMessage);
51-
$importRun->setResult($success);
52-
$importRun->setType(System::class);
53-
54-
$this->entityManager->persist($importRun);
55-
$this->entityManager->flush();
44+
$this->recordImportRun(System::class, $success, $errorMessage);
5645
}
5746
}

0 commit comments

Comments
 (0)