1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the DataImporter package. |
7
|
|
|
* |
8
|
|
|
* (c) Loïc Sapone <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace IQ2i\DataImporter\Bundle\Command; |
15
|
|
|
|
16
|
|
|
use IQ2i\DataImporter\Archiver\ArchiverInterface; |
17
|
|
|
use IQ2i\DataImporter\Bundle\Exception\ItemHandlingException; |
18
|
|
|
use IQ2i\DataImporter\Bundle\Processor\CliProcessor; |
19
|
|
|
use IQ2i\DataImporter\DataImporter; |
20
|
|
|
use IQ2i\DataImporter\Exchange\Message; |
21
|
|
|
use IQ2i\DataImporter\Processor\ProcessorInterface; |
22
|
|
|
use IQ2i\DataImporter\Reader\ReaderInterface; |
23
|
|
|
use Symfony\Component\Console\Command\Command; |
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
29
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
30
|
|
|
|
31
|
|
|
abstract class AbstractImportCommand extends Command |
32
|
|
|
{ |
33
|
|
|
protected InputInterface $input; |
34
|
|
|
|
35
|
|
|
protected OutputInterface $output; |
36
|
|
|
|
37
|
|
|
abstract protected function handleItem(): callable; |
38
|
|
|
|
39
|
|
|
abstract protected function getReader(?string $filename = null): ReaderInterface; |
40
|
|
|
|
41
|
2 |
|
protected function configure(): void |
42
|
|
|
{ |
43
|
2 |
|
$this |
44
|
2 |
|
->addArgument('filename', InputArgument::OPTIONAL, 'File to import') |
45
|
2 |
|
->addOption('step', null, InputOption::VALUE_NONE, 'Step through each record one-by-one') |
46
|
2 |
|
->addOption('pause-on-error', null, InputOption::VALUE_NONE, 'Pause if an exception is thrown') |
47
|
2 |
|
->addOption('batch-size', null, InputOption::VALUE_REQUIRED, 'Batch size', 100) |
48
|
2 |
|
; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
52
|
|
|
{ |
53
|
2 |
|
$this->input = $input; |
54
|
2 |
|
$this->output = $output; |
55
|
|
|
|
56
|
2 |
|
$io = new SymfonyStyle($input, $output); |
57
|
2 |
|
$io->title('Start importing data'); |
58
|
|
|
|
59
|
|
|
try { |
60
|
2 |
|
(new DataImporter( |
61
|
2 |
|
$this->getReader($input->getArgument('filename')), |
62
|
2 |
|
$this->getProcessor($input, $output), |
63
|
2 |
|
$this->getArchiver(), |
|
|
|
|
64
|
2 |
|
$this->getSerializer() |
|
|
|
|
65
|
2 |
|
))->execute(); |
66
|
|
|
} catch (ItemHandlingException $itemHandlingException) { |
67
|
|
|
$io->newLine(2); |
68
|
|
|
$io->error($itemHandlingException->getMessage()); |
69
|
|
|
|
70
|
|
|
return $itemHandlingException->getCode(); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return Command::SUCCESS; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
protected function handleBegin(): callable |
77
|
|
|
{ |
78
|
2 |
|
return static function (Message $message) { |
|
|
|
|
79
|
2 |
|
}; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
protected function handleBatch(): callable |
83
|
|
|
{ |
84
|
2 |
|
return static function (Message $message) { |
|
|
|
|
85
|
2 |
|
}; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
protected function handleEnd(): callable |
89
|
|
|
{ |
90
|
2 |
|
return static function (Message $message, array $errors) { |
|
|
|
|
91
|
2 |
|
}; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
protected function getProcessor(InputInterface $input, OutputInterface $output): ProcessorInterface |
95
|
|
|
{ |
96
|
2 |
|
return new CliProcessor($input, $output, $this->handleBegin(), $this->handleItem(), $this->handleBatch(), $this->handleEnd(), $this->getSerializer()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
protected function getArchiver(): ?ArchiverInterface |
100
|
|
|
{ |
101
|
2 |
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
protected function getSerializer(): ?SerializerInterface |
105
|
|
|
{ |
106
|
2 |
|
return null; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.