-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractImportCommand.php
More file actions
67 lines (58 loc) · 1.76 KB
/
AbstractImportCommand.php
File metadata and controls
67 lines (58 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace App\Command;
use App\Entity\ImportRun;
use App\Service\BaseImporter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
abstract class AbstractImportCommand extends Command
{
public function __construct(
protected BaseImporter $importer,
protected EntityManagerInterface $entityManager)
{
parent::__construct();
}
/**
* Run the importer.
*
* @throws \Exception
*/
protected function import(string $type, string $src, OutputInterface $output, bool $progress = false): void
{
$success = true;
$errorMessage = null;
$progressBar = $progress ? new ProgressBar($output) : null;
try {
$this->importer->import($src, $progressBar);
} catch (\Exception $e) {
$success = false;
$errorMessage = $e->getMessage();
$output->writeln($errorMessage);
}
$this->recordImportRun($type, $success, $errorMessage);
}
/**
* Record import run.
*
* @param string $type
* The type of the import
* @param bool $success
* Success of run
* @param string|null $output
* Output message or null
*
* @throws \Exception
*/
protected function recordImportRun(string $type, bool $success, ?string $output = null): void
{
$importRun = new ImportRun();
$importRun->setDatetime(new \DateTime());
$importRun->setOutput($output);
$importRun->setResult($success);
$importRun->setType($type);
$this->entityManager->persist($importRun);
$this->entityManager->flush();
}
}