/**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return null;
     }
     $metric = $this->factory->createMetric($context['attribute']->getMetricFamily());
     $metric->setData($this->localizer->localize($data['data'], $context));
     $metric->setUnit($data['unit']);
     return $metric;
 }
 /**
  * The metric is built by many ordered calls, one for the data column, one for the unit column
  *
  * @param ProductValueInterface $value
  * @param string                $dataOrUnit
  *
  * @return MetricInterface
  */
 protected function addFromManyFields(ProductValueInterface $value, $dataOrUnit)
 {
     // TODO come from original implementation, really FRAGIL because depends on many ordered calls
     if (null === ($metric = $value->getMetric())) {
         $metric = $this->factory->createMetric($value->getAttribute()->getMetricFamily());
         $metric->setData($dataOrUnit);
     } else {
         $metric->setUnit($dataOrUnit);
     }
     return $metric;
 }
 /**
  * @param ProductInterface   $fromProduct
  * @param ProductInterface   $toProduct
  * @param AttributeInterface $fromAttribute
  * @param AttributeInterface $toAttribute
  * @param string             $fromLocale
  * @param string             $toLocale
  * @param string             $fromScope
  * @param string             $toScope
  */
 protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope)
 {
     $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope);
     if (null !== $fromValue) {
         $fromData = $fromValue->getData();
         $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope);
         if (null === $toValue) {
             $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope);
         }
         if (null === ($metric = $toValue->getMetric())) {
             $metric = $this->metricFactory->createMetric($fromData->getFamily());
         }
         $metric->setUnit($fromData->getUnit());
         $metric->setData($fromData->getData());
         $toValue->setMetric($metric);
     }
 }
 /**
  * Set the data into the product value
  *
  * @param ProductInterface   $product
  * @param AttributeInterface $attribute
  * @param mixed              $data
  * @param string             $unit
  * @param string             $locale
  * @param string             $scope
  */
 protected function setData(ProductInterface $product, AttributeInterface $attribute, $data, $unit, $locale, $scope)
 {
     $value = $product->getValue($attribute->getCode(), $locale, $scope);
     if (null === $value) {
         $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope);
     }
     if (null === ($metric = $value->getMetric())) {
         $metric = $this->metricFactory->createMetric($attribute->getMetricFamily());
     }
     $value->setMetric($metric);
     $metric->setUnit($unit);
     $metric->setData($data);
 }