You are here

class AdjustmentTransformer in Commerce Core 8.2

Hierarchy

Expanded class hierarchy of AdjustmentTransformer

1 string reference to 'AdjustmentTransformer'
commerce_order.services.yml in modules/order/commerce_order.services.yml
modules/order/commerce_order.services.yml
1 service uses AdjustmentTransformer
commerce_order.adjustment_transformer in modules/order/commerce_order.services.yml
Drupal\commerce_order\AdjustmentTransformer

File

modules/order/src/AdjustmentTransformer.php, line 8

Namespace

Drupal\commerce_order
View source
class AdjustmentTransformer implements AdjustmentTransformerInterface {

  /**
   * The adjustment type manager.
   *
   * @var \Drupal\commerce_order\AdjustmentTypeManager
   */
  protected $adjustmentTypeManager;

  /**
   * The rounder.
   *
   * @var \Drupal\commerce_price\RounderInterface
   */
  protected $rounder;

  /**
   * Constructs a new AdjustmentTransformer object.
   *
   * @param \Drupal\commerce_order\AdjustmentTypeManager $adjustment_type_manager
   *   The adjustment type manager.
   * @param \Drupal\commerce_price\RounderInterface $rounder
   *   The rounder.
   */
  public function __construct(AdjustmentTypeManager $adjustment_type_manager, RounderInterface $rounder) {
    $this->adjustmentTypeManager = $adjustment_type_manager;
    $this->rounder = $rounder;
  }

  /**
   * {@inheritdoc}
   */
  public function processAdjustments(array $adjustments) {
    $adjustments = $this
      ->combineAdjustments($adjustments);
    $adjustments = $this
      ->sortAdjustments($adjustments);
    $adjustments = $this
      ->roundAdjustments($adjustments);
    return $adjustments;
  }

  /**
   * {@inheritdoc}
   */
  public function combineAdjustments(array $adjustments) {
    $combined_adjustments = [];
    foreach ($adjustments as $index => $adjustment) {
      $type = $adjustment
        ->getType();
      $source_id = $adjustment
        ->getSourceId();
      if (empty($source_id)) {

        // Adjustments without a source ID are always shown standalone.
        $key = $index;
      }
      else {

        // Adjustments with the same type and source ID are combined.
        $key = $type . '_' . $source_id;
      }
      if (empty($combined_adjustments[$key])) {
        $combined_adjustments[$key] = $adjustment;
      }
      else {
        $combined_adjustments[$key] = $combined_adjustments[$key]
          ->add($adjustment);
      }
    }

    // The keys used for combining are irrelevant to the caller.
    $combined_adjustments = array_values($combined_adjustments);
    return $combined_adjustments;
  }

  /**
   * {@inheritdoc}
   */
  public function sortAdjustments(array $adjustments) {
    $types = $this->adjustmentTypeManager
      ->getDefinitions();
    $data = [];
    foreach ($adjustments as $adjustment) {
      $data[] = [
        'adjustment' => $adjustment,
        'weight' => $types[$adjustment
          ->getType()]['weight'],
      ];
    }
    uasort($data, [
      SortArray::class,
      'sortByWeightElement',
    ]);

    // Re-extract the adjustments from the sorted array.
    $adjustments = array_column($data, 'adjustment');
    return $adjustments;
  }

  /**
   * {@inheritdoc}
   */
  public function roundAdjustments(array $adjustments, $mode = PHP_ROUND_HALF_UP) {
    foreach ($adjustments as $index => $adjustment) {
      $adjustments[$index] = $this
        ->roundAdjustment($adjustment, $mode);
    }
    return $adjustments;
  }

  /**
   * {@inheritdoc}
   */
  public function roundAdjustment(Adjustment $adjustment, $mode = PHP_ROUND_HALF_UP) {
    $amount = $this->rounder
      ->round($adjustment
      ->getAmount(), $mode);
    $adjustment = new Adjustment([
      'amount' => $amount,
    ] + $adjustment
      ->toArray());
    return $adjustment;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdjustmentTransformer::$adjustmentTypeManager protected property The adjustment type manager.
AdjustmentTransformer::$rounder protected property The rounder.
AdjustmentTransformer::combineAdjustments public function Combines adjustments with the same type and source ID. Overrides AdjustmentTransformerInterface::combineAdjustments
AdjustmentTransformer::processAdjustments public function Combines, sorts, and rounds the given adjustments. Overrides AdjustmentTransformerInterface::processAdjustments
AdjustmentTransformer::roundAdjustment public function Rounds an adjustment to its currency precision. Overrides AdjustmentTransformerInterface::roundAdjustment
AdjustmentTransformer::roundAdjustments public function Rounds adjustments to their currency precision. Overrides AdjustmentTransformerInterface::roundAdjustments
AdjustmentTransformer::sortAdjustments public function Sorts adjustments by their type's weight. Overrides AdjustmentTransformerInterface::sortAdjustments
AdjustmentTransformer::__construct public function Constructs a new AdjustmentTransformer object.