/** * Extract tax data from the tax response payload and store tax records, * duties and fees. * * Extracts all three sets of tax data as each set of data can be retrieved * from the same address parser. Extracting all three sets at once prevents * nearly identical steps from being repeated for each ship group for each * type of tax data. * * @return self */ protected function _extractTaxData() { // Each of these will hold an array of arrays of data extracted from each // ship group - e.g. $taxRecords = [[$recordA, $recordB], [$recordC, $recordD]]. $taxRecords = []; $duties = []; $fees = []; foreach ($this->_taxResponse->getShipGroups() as $shipGroup) { $address = $this->_getQuoteAddressForShipGroup($shipGroup); if ($address) { $addressParser = $this->_taxFactory->createResponseAddressParser($shipGroup, $address); $taxRecords[] = $addressParser->getTaxRecords(); $duties[] = $addressParser->getTaxDuties(); $fees[] = $addressParser->getTaxFees(); } else { $this->_logger->warn('Tax response ship group does not relate to any known address.', $this->_logContext->getMetaData(__CLASS__, ['rom_response_body' => $shipGroup->serialize()])); } } // Flatten each nested array of tax data - allows for a single array_merge // instead of iteratively calling array_merge on each pass when extracting // tax data for each ship group. $this->_taxRecords = $this->_flattenArray($taxRecords); $this->_taxDuties = $this->_flattenArray($duties); $this->_taxFees = $this->_flattenArray($fees); return $this; }