You are here

private function GenerateProducts::getRandomPrice in Commerce Bulk 8

1 call to GenerateProducts::getRandomPrice()
GenerateProducts::generateSaveProduct in modules/commerce_generate/src/Plugin/DevelGenerate/GenerateProducts.php
Create one product. Used by both batch and non-batch code branches.

File

modules/commerce_generate/src/Plugin/DevelGenerate/GenerateProducts.php, line 423

Class

GenerateProducts
Provides a GenerateProducts plugin.

Namespace

Drupal\commerce_generate\Plugin\DevelGenerate

Code

private function getRandomPrice(&$values, $list = '') {
  $min = $values["{$list}price_min"];
  $max = $values["{$list}price_max"];
  $min = bccomp($min, $max) === -1 ? $min : $max;
  $max = bccomp($max, $min) === 1 ? $max : $min;
  if (bccomp($min, $max) === -1) {
    $min_decimals = explode('.', $min);
    $min_decimals = isset($min_decimals[1]) ? $min_decimals[1] + 0 : 1;
    $max_decimals = explode('.', $max);
    $max_decimals = isset($max_decimals[1]) ? $max_decimals[1] + 0 : 99;
    $decimals = rand($min_decimals ?: 1, $max_decimals ?: 99);
    $decimals = $decimals < 10 ? rand(0, 1) . $decimals : $decimals;
    $number = $values['price_number'] = rand($min, $max) . '.' . $decimals;
  }
  else {
    $number = $values['price_number'] = bcadd($max, 0);
  }

  // If some crazy numbers are submited then it may return scientific notation
  // number which is not supported by this module. So, to prevent crashes
  // return something sensible instead.
  return is_numeric($number) && $number > 0 ? $number : '1.11';
}