/**
* {@inheritdoc}
* @see ApiEntityAdapter::validateEntity()
* @param Entity\FarmRoleScalingMetric $entity scaling metric entity
*/
public function validateEntity($entity)
{
if (!$entity instanceof Entity\FarmRoleScalingMetric) {
throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\FarmRoleScalingMetric class"));
}
$farmRole = $entity->getFarmRole();
if (empty($farmRole)) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Farm role with ID: %d", $entity->farmRoleId));
}
$disabledScalingBehaviors = array_intersect(FarmRoleAdapter::$disableScalingBehaviors, $farmRole->getRole()->getBehaviors());
if (!empty($disabledScalingBehaviors)) {
throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, sprintf('Can not add Scaling Metric to the Role with the following built-in automation types: %s.', implode(', ', RoleAdapter::behaviorsToData($disabledScalingBehaviors))));
}
if (empty($entity->id)) {
if (empty($entity->metricId)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property name");
}
$criteria = [['farmRoleId' => $entity->farmRoleId], ['metricId' => $entity->metricId]];
if (!empty(Entity\FarmRoleScalingMetric::findOne($criteria))) {
throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, 'This Scaling metric already added to the current Farm role.');
}
}
}