You are here

public function ShipmentWeightMultipleConds::evaluate in Commerce Shipping Weight Tariff 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Commerce/Condition/ShipmentWeightMultipleConds.php \Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\Condition\ShipmentWeightMultipleConds::evaluate()

Evaluates shipment weight conditions.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Shipment entity object.

Return value

bool Returns entity evaluation result.

Overrides ConditionInterface::evaluate

File

src/Plugin/Commerce/Condition/ShipmentWeightMultipleConds.php, line 254

Class

ShipmentWeightMultipleConds
Provides the multiple weight condition for shipments.

Namespace

Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\Condition

Code

public function evaluate(EntityInterface $entity) {
  $this
    ->assertEntity($entity);

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
  $shipment = $entity;
  $weight = $shipment
    ->getWeight();
  if (!$weight) {

    // The conditions can't be applied until the weight is known.
    return FALSE;
  }

  // Get enabled conditions list.
  $enabled_conditions = [];
  foreach ($this
    ->getConfiguration() as $config) {
    if (!empty($config['enabled'])) {
      $enabled_conditions[] = $config;
    }
  }

  // Evaluate matching conditions.
  if (count($enabled_conditions) === 1) {
    $condition_unit = $enabled_conditions['0']['weight']['unit'];

    /** @var \Drupal\physical\Weight $weight */
    $weight = $weight
      ->convert($condition_unit);
    $condition_weight = new Weight($enabled_conditions['0']['weight']['number'], $condition_unit);

    // Saving condition info to states.
    $this->stateService
      ->set('order_' . $shipment
      ->getOrderId() . '_weight_condition', $enabled_conditions['0']);

    // Return evaluation results.
    switch ($enabled_conditions['0']['operator']) {
      case '>=':
        return $weight
          ->greaterThanOrEqual($condition_weight);
      case '>':
        return $weight
          ->greaterThan($condition_weight);
      case '<=':
        return $weight
          ->lessThanOrEqual($condition_weight);
      case '<':
        return $weight
          ->lessThan($condition_weight);
      case '==':
        return $weight
          ->equals($condition_weight);
      default:
        throw new \InvalidArgumentException("Invalid operator {$this->configuration['operator']}");
    }
  }
  else {

    // Get matched conditions.
    $matched_conditions = [];
    foreach ($enabled_conditions as $key => $condition) {
      $condition_unit = $condition['weight']['unit'];

      /** @var \Drupal\physical\Weight $weight */
      $weight = $weight
        ->convert($condition_unit);
      $condition_weight = new Weight($condition['weight']['number'], $condition_unit);
      switch ($condition['operator']) {
        case '>=':
          $matched_conditions[$key]['condition_type'] = 'greaterThanOrEqual';
          $matched_conditions[$key]['condition_result'] = $weight
            ->greaterThanOrEqual($condition_weight);
          $matched_conditions[$key]['condition_weight'] = $condition['weight']['number'];
          $matched_conditions[$key]['condition_price'] = $condition['price']['number'];
          $matched_conditions[$key]['condition_operator'] = $condition['operator'];
          break;
        case '>':
          $matched_conditions[$key]['condition_type'] = 'greaterThan';
          $matched_conditions[$key]['condition_result'] = $weight
            ->greaterThan($condition_weight);
          $matched_conditions[$key]['condition_weight'] = $condition['weight']['number'];
          $matched_conditions[$key]['condition_price'] = $condition['price']['number'];
          $matched_conditions[$key]['condition_operator'] = $condition['operator'];
          break;
        case '<=':
          $matched_conditions[$key]['condition_type'] = 'lessThanOrEqual';
          $matched_conditions[$key]['condition_result'] = $weight
            ->lessThanOrEqual($condition_weight);
          $matched_conditions[$key]['condition_weight'] = $condition['weight']['number'];
          $matched_conditions[$key]['condition_price'] = $condition['price']['number'];
          $matched_conditions[$key]['condition_operator'] = $condition['operator'];
          break;
        case '<':
          $matched_conditions[$key]['condition_type'] = 'lessThan';
          $matched_conditions[$key]['condition_result'] = $weight
            ->lessThan($condition_weight);
          $matched_conditions[$key]['condition_weight'] = $condition['weight']['number'];
          $matched_conditions[$key]['condition_price'] = $condition['price']['number'];
          $matched_conditions[$key]['condition_operator'] = $condition['operator'];
          break;
        case '==':
          $matched_conditions[$key]['condition_type'] = 'equals';
          $matched_conditions[$key]['condition_result'] = $weight
            ->equals($condition_weight);
          $matched_conditions[$key]['condition_weight'] = $condition['weight']['number'];
          $matched_conditions[$key]['condition_price'] = $condition['price']['number'];
          $matched_conditions[$key]['condition_operator'] = $condition['operator'];
          break;
        default:
          throw new \InvalidArgumentException("Invalid operator {$this->configuration['operator']}");
      }
    }
    foreach ($matched_conditions as $match) {
      if ($match['condition_result'] === TRUE) {

        // Saving condition info to states.
        $this->stateService
          ->set('order_' . $shipment
          ->getOrderId() . '_weight_condition', $match);

        // Return evaluation results.
        return $weight
          ->{$match['condition_type']}($condition_weight);
      }
    }
  }
  return FALSE;
}