/**
  * Collect applied tax rates information on address level
  *
  * @param QuoteAddress\Total $total
  * @param array $applied
  * @param float $amount
  * @param float $baseAmount
  * @param float $rate
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _saveAppliedTaxes(QuoteAddress\Total $total, $applied, $amount, $baseAmount, $rate)
 {
     $previouslyAppliedTaxes = $total->getAppliedTaxes();
     $process = count($previouslyAppliedTaxes);
     foreach ($applied as $row) {
         if ($row['percent'] == 0) {
             continue;
         }
         if (!isset($previouslyAppliedTaxes[$row['id']])) {
             $row['process'] = $process;
             $row['amount'] = 0;
             $row['base_amount'] = 0;
             $previouslyAppliedTaxes[$row['id']] = $row;
         }
         if (!is_null($row['percent'])) {
             $row['percent'] = $row['percent'] ? $row['percent'] : 1;
             $rate = $rate ? $rate : 1;
             $appliedAmount = $amount / $rate * $row['percent'];
             $baseAppliedAmount = $baseAmount / $rate * $row['percent'];
         } else {
             $appliedAmount = 0;
             $baseAppliedAmount = 0;
             foreach ($row['rates'] as $rate) {
                 $appliedAmount += $rate['amount'];
                 $baseAppliedAmount += $rate['base_amount'];
             }
         }
         if ($appliedAmount || $previouslyAppliedTaxes[$row['id']]['amount']) {
             $previouslyAppliedTaxes[$row['id']]['amount'] += $appliedAmount;
             $previouslyAppliedTaxes[$row['id']]['base_amount'] += $baseAppliedAmount;
         } else {
             unset($previouslyAppliedTaxes[$row['id']]);
         }
     }
     $total->setAppliedTaxes($previouslyAppliedTaxes);
 }