You are here

public function ShipmentWeight::evaluate in Commerce Shipping 8.2

Evaluates the condition.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

Return value

bool TRUE if the condition has been met, FALSE otherwise.

Overrides ConditionInterface::evaluate

File

src/Plugin/Commerce/Condition/ShipmentWeight.php, line 73

Class

ShipmentWeight
Provides the weight condition for shipments.

Namespace

Drupal\commerce_shipping\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;
  }
  $condition_unit = $this->configuration['weight']['unit'];

  /** @var \Drupal\physical\Weight $weight */
  $weight = $weight
    ->convert($condition_unit);
  $condition_weight = new Weight($this->configuration['weight']['number'], $condition_unit);
  switch ($this->configuration['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']}");
  }
}