/**
  * Allow to create convert data in standard unit for metrics
  *
  * @param AbstractMetric $metric
  */
 protected function createMetricBaseValues(AbstractMetric $metric)
 {
     $baseUnit = $this->manager->getStandardUnitForFamily($metric->getFamily());
     if (is_numeric($metric->getData())) {
         $baseData = $this->converter->setFamily($metric->getFamily())->convertBaseToStandard($metric->getUnit(), $metric->getData());
     } else {
         $baseData = null;
     }
     $metric->setBaseData($baseData)->setBaseUnit($baseUnit);
 }
 /**
  * Convert all the products metric values into the channel configured conversion units
  *
  * @param ProductInterface $product
  * @param ChannelInterface $channel
  */
 public function convert(ProductInterface $product, ChannelInterface $channel)
 {
     $channelUnits = $channel->getConversionUnits();
     foreach ($product->getValues() as $value) {
         $data = $value->getData();
         $attribute = $value->getAttribute();
         if ($data instanceof MetricInterface && isset($channelUnits[$attribute->getCode()])) {
             $channelUnit = $channelUnits[$attribute->getCode()];
             $this->converter->setFamily($data->getFamily());
             $data->setData($this->converter->convert($data->getUnit(), $channelUnit, $data->getData()));
             $data->setUnit($channelUnit);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function apply(FilterDatasourceAdapterInterface $ds, $data)
 {
     $data = $this->parseData($data);
     if (!$data) {
         return false;
     }
     $operator = $this->getOperator($data['type']);
     $ds->generateParameterName($this->getName());
     // Convert value to base unit
     if ('EMPTY' !== $operator) {
         $this->converter->setFamily($this->family);
         $baseValue = $this->converter->convertBaseToStandard($data['unit'], $data['value']);
     } else {
         $baseValue = null;
     }
     $this->util->applyFilterByAttribute($ds, $this->get(ProductFilterUtility::DATA_NAME_KEY), $baseValue, $operator);
     return true;
 }
 /**
  * @param AttributeInterface $attribute
  * @param array              $data
  *
  * @return float
  */
 protected function convertValue(AttributeInterface $attribute, array $data)
 {
     $this->measureConverter->setFamily($attribute->getMetricFamily());
     return $this->measureConverter->convertBaseToStandard($data['unit'], $data['data']);
 }
 /**
  * Test related exception in standard to final method
  * @expectedException Akeneo\Bundle\MeasureBundle\Exception\UnknownMeasureException
  */
 public function testUnknownMeasureExceptionStandardToFinal()
 {
     $this->converter->setFamily(WeightFamilyInterface::FAMILY);
     $this->converter->convert(WeightFamilyInterface::GRAM, 'test-unit', 50);
 }