protected function scheduleToEntity($object, FarmRoleScalingMetric $entity)
{
if (empty($object->schedule)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property schedule');
}
if (!is_array($object->schedule)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Property schedule must be an array");
}
$schedulePeriods = [];
$entity->settings = new ObjectAccess();
foreach ($object->schedule as $collection) {
$schedule = [];
foreach (static::$dateTimeSettingsMap as $key => $property) {
if (!isset($collection->{$property})) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, sprintf('Missed property schedule.%s', $property));
}
if ($key == FarmRoleScalingMetric::START_TIME || $key == FarmRoleScalingMetric::END_TIME) {
$schedule[$key] = static::convertInputValue('datetime', $collection->{$property}, $property);
$this->validateTimeInterval($schedule[$key]);
} else {
$schedule[$key] = $collection->{$property};
}
}
if (!is_array($schedule[FarmRoleScalingMetric::WEEK_DAYS])) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Property daysOfWeek must be an array");
}
$diffDays = array_diff(array_map('ucfirst', $schedule[FarmRoleScalingMetric::WEEK_DAYS]), static::$listOfWeakDays);
if (!empty($diffDays)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf('The %s %s of property daysOfWeek %s not valid', ...count($diffDays) > 1 ? ['values', implode(', ', $diffDays), 'are'] : ['value', array_shift($diffDays), 'is']));
}
$this->validateNumericSetting($schedule, FarmRoleScalingMetric::INSTANCES_COUNT, static::$dateTimeSettingsMap[FarmRoleScalingMetric::INSTANCES_COUNT]);
/* @var $start DateTime */
$start = $schedule[FarmRoleScalingMetric::START_TIME];
/* @var $end DateTime */
$end = $schedule[FarmRoleScalingMetric::END_TIME];
$intStart = (int) $start->format(FarmRoleScalingMetric::SCALING_TIME_FORMAT);
$intEnd = (int) $end->format(FarmRoleScalingMetric::SCALING_TIME_FORMAT);
if ($intEnd <= $intStart) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf('End time value %s must be greater than Start time value %s', $end->format(FarmRoleScalingMetric::SETTINGS_TIME_FORMAT), $start->format(FarmRoleScalingMetric::SETTINGS_TIME_FORMAT)));
}
$currentPeriod = [FarmRoleScalingMetric::START_TIME => $intStart, FarmRoleScalingMetric::END_TIME => $intEnd, FarmRoleScalingMetric::WEEK_DAYS => $schedule[FarmRoleScalingMetric::WEEK_DAYS]];
foreach ($schedulePeriods as $key => $schedulePeriod) {
if ($currentPeriod[FarmRoleScalingMetric::START_TIME] <= $schedulePeriod[FarmRoleScalingMetric::END_TIME] && $currentPeriod[FarmRoleScalingMetric::END_TIME] >= $schedulePeriod[FarmRoleScalingMetric::START_TIME] && !empty(array_intersect($schedulePeriod[FarmRoleScalingMetric::WEEK_DAYS], $currentPeriod[FarmRoleScalingMetric::WEEK_DAYS]))) {
$ovSchedule = $entity->settings[$key];
throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Period %s %s - %s overlaps with period %s %s - %s', implode(', ', $schedule[FarmRoleScalingMetric::WEEK_DAYS]), $start->format(FarmRoleScalingMetric::SETTINGS_TIME_FORMAT), $end->format(FarmRoleScalingMetric::SETTINGS_TIME_FORMAT), implode(', ', $ovSchedule[FarmRoleScalingMetric::WEEK_DAYS]), $ovSchedule[FarmRoleScalingMetric::START_TIME]->format(FarmRoleScalingMetric::SETTINGS_TIME_FORMAT), $ovSchedule[FarmRoleScalingMetric::END_TIME]->format(FarmRoleScalingMetric::SETTINGS_TIME_FORMAT)));
}
}
$schedulePeriods[] = $currentPeriod;
$entity->settings[] = $schedule;
}
}