You are here

class Rounder in Commerce Core 8.2

Hierarchy

Expanded class hierarchy of Rounder

1 file declares its use of Rounder
RounderTest.php in modules/price/tests/src/Unit/RounderTest.php
1 string reference to 'Rounder'
commerce_price.services.yml in modules/price/commerce_price.services.yml
modules/price/commerce_price.services.yml
1 service uses Rounder
commerce_price.rounder in modules/price/commerce_price.services.yml
Drupal\commerce_price\Rounder

File

modules/price/src/Rounder.php, line 7

Namespace

Drupal\commerce_price
View source
class Rounder implements RounderInterface {

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $currencyStorage;

  /**
   * Constructs a new Rounder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->currencyStorage = $entity_type_manager
      ->getStorage('commerce_currency');
  }

  /**
   * {@inheritdoc}
   */
  public function round(Price $price, $mode = PHP_ROUND_HALF_UP) {
    $currency_code = $price
      ->getCurrencyCode();

    /** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
    $currency = $this->currencyStorage
      ->load($currency_code);
    if (!$currency) {
      throw new \InvalidArgumentException(sprintf('Could not load the "%s" currency.', $currency_code));
    }
    $rounded_number = Calculator::round($price
      ->getNumber(), $currency
      ->getFractionDigits(), $mode);
    return new Price($rounded_number, $currency_code);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Rounder::$currencyStorage protected property The currency storage.
Rounder::round public function Rounds the given price to its currency precision. Overrides RounderInterface::round
Rounder::__construct public function Constructs a new Rounder object.