1 | <?php |
||
24 | class ImportCommand extends ContainerAwareCommand |
||
25 | { |
||
26 | |||
27 | const MAX_VIOLATION_ERRORS = 10; |
||
28 | |||
29 | 4 | protected function configure() |
|
41 | |||
42 | 4 | protected function validateInput(InputInterface $input) |
|
|
|||
43 | { |
||
44 | 4 | if (!$this->getContainer()->has('mathielen_importengine.import.builder') || |
|
45 | 4 | !$this->getContainer()->has('mathielen_importengine.import.runner')) { |
|
46 | throw new InvalidConfigurationException("No importengine services have been found. Did you register the bundle in AppKernel and configured at least one importer in config?"); |
||
47 | } |
||
48 | 4 | } |
|
49 | |||
50 | 4 | protected function execute(InputInterface $input, OutputInterface $output) |
|
68 | |||
69 | 4 | protected function import(OutputInterface $output, $importerId, $sourceProviderId, $sourceId, $context=null, $limit=null, $isDryrun=false) |
|
70 | { |
||
71 | 4 | $output->writeln("Commencing ".($isDryrun?'<comment>dry-run</comment> ':'')."import using importer ".(empty($importerId)?'<comment>unknown</comment>':"<info>$importerId</info>")." with source provider <info>$sourceProviderId</info> and source id <info>$sourceId</info>"); |
|
72 | |||
73 | 4 | $sourceId = Utils::parseSourceId($sourceId); |
|
74 | 4 | $progress = new ProgressBar($output); |
|
75 | |||
76 | //set limit |
||
77 | 4 | if ($limit) { |
|
78 | $this->getContainer()->get('event_dispatcher')->addListener(ImportConfigureEvent::AFTER_BUILD, function (ImportConfigureEvent $event) use ($limit) { |
||
79 | $event->getImport()->importer()->filters()->add(new OffsetFilter(0, $limit)); |
||
80 | 1 | }); |
|
81 | 1 | } |
|
82 | |||
83 | //show discovered importer id |
||
84 | 4 | if (empty($importerId)) { |
|
85 | $this->getContainer()->get('event_dispatcher')->addListener(ImportRequestEvent::DISCOVERED, function (ImportRequestEvent $event) use ($output) { |
||
86 | $importerId = $event->getImportRequest()->getImporterId(); |
||
87 | $output->writeln("Importer discovered: <info>$importerId</info>"); |
||
88 | 2 | }); |
|
89 | 2 | } |
|
90 | |||
91 | /** @var ImportBuilder $importBuilder */ |
||
92 | 4 | $importBuilder = $this->getContainer()->get('mathielen_importengine.import.builder'); |
|
93 | |||
94 | 4 | $importRequest = new ImportRequest($sourceId, $sourceProviderId, $importerId, Utils::whoAmI().'@CLI'); |
|
95 | |||
96 | 4 | $import = $importBuilder->buildFromRequest($importRequest); |
|
97 | |||
98 | //apply context info from commandline |
||
99 | 4 | $importRun = $import->getRun(); |
|
100 | 4 | $importRun->setContext($context); |
|
101 | |||
102 | //status callback |
||
103 | 4 | $this->getContainer()->get('event_dispatcher')->addListener(ImportItemEvent::AFTER_READ, function (ImportItemEvent $event) use ($output, &$progress) { |
|
104 | /** @var ImportRun $importRun */ |
||
105 | $importRun = $event->getContext()->getRun(); |
||
106 | $stats = $importRun->getStatistics(); |
||
107 | $processed = isset($stats['processed'])?$stats['processed']:0; |
||
108 | $max = $importRun->getInfo()['count']; |
||
109 | |||
110 | if ($progress->getMaxSteps() != $max) { |
||
111 | $progress = new ProgressBar($output, $max); |
||
112 | $progress->start(); |
||
113 | } |
||
114 | |||
115 | $progress->setProgress($processed); |
||
116 | 4 | }); |
|
117 | |||
118 | /** @var ImportRunner $importRunner */ |
||
119 | 4 | $importRunner = $this->getContainer()->get('mathielen_importengine.import.runner'); |
|
120 | 4 | if ($isDryrun) { |
|
121 | 1 | $importRunner->dryRun($import); |
|
122 | 1 | } else { |
|
123 | 3 | $importRunner->run($import); |
|
124 | } |
||
125 | |||
126 | 4 | $progress->finish(); |
|
127 | 4 | $output->writeln(''); |
|
128 | 4 | $output->writeln("<info>Import done</info>"); |
|
129 | 4 | $output->writeln(''); |
|
130 | |||
131 | 4 | $this->writeStatistics($importRun->getStatistics(), new Table($output)); |
|
132 | |||
133 | 4 | $this->writeValidationViolations( |
|
134 | $import |
||
135 | 4 | ->importer() |
|
136 | 4 | ->validation() |
|
137 | 4 | ->getViolations(), |
|
138 | 4 | new Table($output)); |
|
139 | |||
140 | 4 | $output->writeln(''); |
|
141 | 4 | } |
|
142 | |||
143 | 4 | protected function writeValidationViolations(array $violations, Table $table) |
|
183 | |||
184 | 4 | protected function writeStatistics(array $statistics, Table $table) |
|
197 | |||
198 | } |
||
199 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.