55namespace App \Command ;
66
77use Akeneo \PimEnterprise \ApiClient \AkeneoPimEnterpriseClientInterface ;
8+ use App \ApiClientFactory ;
89use App \FileLogger ;
910use App \Processor \Converter \DataConverter ;
1011use App \Processor \RecordProcessor ;
@@ -42,9 +43,6 @@ class ImportCommand extends Command
4243 /** @var StructureGenerator */
4344 private $ structureGenerator ;
4445
45- /** @var AkeneoPimEnterpriseClientBuilder */
46- private $ clientBuilder ;
47-
4846 /** @var DataConverter */
4947 private $ converter ;
5048
@@ -68,20 +66,20 @@ class ImportCommand extends Command
6866
6967 public function __construct (
7068 StructureGenerator $ structureGenerator ,
71- AkeneoPimEnterpriseClientBuilder $ clientBuilder ,
7269 DataConverter $ converter ,
7370 RecordProcessor $ processor ,
7471 FileLogger $ logger ,
75- InvalidFileGenerator $ invalidFileGenerator
72+ InvalidFileGenerator $ invalidFileGenerator ,
73+ AkeneoPimEnterpriseClientInterface $ apiClient
7674 ) {
7775 parent ::__construct (static ::$ defaultName );
7876
7977 $ this ->structureGenerator = $ structureGenerator ;
80- $ this ->clientBuilder = $ clientBuilder ;
8178 $ this ->converter = $ converter ;
8279 $ this ->processor = $ processor ;
8380 $ this ->logger = $ logger ;
8481 $ this ->invalidFileGenerator = $ invalidFileGenerator ;
82+ $ this ->apiClient = $ apiClient ;
8583 }
8684
8785 protected function configure ()
@@ -126,8 +124,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
126124 'If you want to automate this process or don \'t want to use default values, add the --no-interaction flag when you call this command. '
127125 ]);
128126
129- $ this ->initializeApiClient ($ input );
130-
131127 $ this ->io ->newLine (2 );
132128 $ this ->io ->title (
133129 sprintf (
@@ -168,16 +164,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
168164 }
169165 }
170166
171- private function initializeApiClient (InputInterface $ input ): void
172- {
173- $ this ->apiClient = $ this ->clientBuilder ->buildAuthenticatedByPassword (
174- $ input ->getOption ('apiClientId ' ),
175- $ input ->getOption ('apiClientSecret ' ),
176- $ input ->getOption ('apiUsername ' ),
177- $ input ->getOption ('apiPassword ' )
178- );
179- }
180-
181167 private function fetchReferenceEntityAttributes (string $ referenceEntityCode ): array
182168 {
183169 $ attributes = $ this ->apiClient ->getReferenceEntityAttributeApi ()->all ($ referenceEntityCode );
@@ -259,12 +245,12 @@ private function importRecords(
259245 continue ;
260246 }
261247
262- if (count ( $ this ->reader -> getHeaders ()) !== count ($ row )) {
263- $ this -> logger -> skip ( sprintf (
248+ if (! $ this ->isHeaderValid ($ row )) {
249+ $ message = sprintf (
264250 'Skipped line %s: the number of values is not equal to the number of headers ' ,
265251 $ lineNumber
266- )) ;
267- $ this ->invalidFileGenerator -> fromRow ( $ row , $ filePath , $ this -> reader -> getHeaders () );
252+ );
253+ $ this ->skipRowWithMessage ( $ filePath , $ row , $ message );
268254
269255 continue ;
270256 }
@@ -275,7 +261,13 @@ private function importRecords(
275261 $ structure = $ this ->structureGenerator ->generate ($ attributes , $ channels );
276262 $ validStructure = array_intersect_key ($ structure , array_flip ($ validHeaders ));
277263
278- $ recordsToWrite [] = $ this ->processor ->process ($ line , $ validStructure );
264+ try {
265+ $ recordsToWrite [] = $ this ->processor ->process ($ line , $ validStructure , $ filePath );
266+ } catch (\Exception $ e ) {
267+ $ this ->skipRowWithMessage ($ filePath , $ row , $ e ->getMessage ());
268+
269+ continue ;
270+ }
279271 $ linesToWrite [] = $ line ;
280272
281273 if (count ($ recordsToWrite ) === self ::BATCH_SIZE ) {
@@ -307,4 +299,15 @@ private function writeRecords(
307299 $ this ->logger ->logResponses ($ responses );
308300 $ this ->invalidFileGenerator ->fromResponses ($ responses , $ linesToWrite , $ filePath , $ this ->reader ->getHeaders ());
309301 }
302+
303+ private function isHeaderValid ($ row ): bool
304+ {
305+ return count ($ this ->reader ->getHeaders ()) === count ($ row );
306+ }
307+
308+ private function skipRowWithMessage (string $ filePath , $ row , string $ message ): void
309+ {
310+ $ this ->logger ->skip ($ message );
311+ $ this ->invalidFileGenerator ->fromRow ($ row , $ filePath , $ this ->reader ->getHeaders ());
312+ }
310313}
0 commit comments