Refactor something :-)

This commit is contained in:
Olivier PEREZ 2015-11-03 21:17:00 +01:00
parent 40f2a1729c
commit c3b48b885d

View File

@ -277,34 +277,29 @@ class AdminPollService {
* *
* @param $slots array All the slots of the poll * @param $slots array All the slots of the poll
* @param $datetime int The datetime of the new slot * @param $datetime int The datetime of the new slot
* @return null|\stdClass An object like this one: {insert:X, slot:Y} where Y can be null. * @return \stdClass An object like this one: {insert:X, slot:Y} where Y can be null.
*/ */
private function findInsertPosition($slots, $datetime) { private function findInsertPosition($slots, $datetime) {
$result = new \stdClass(); $result = new \stdClass();
$result->slot = null; $result->slot = null;
$result->insert = -1; $result->insert = 0;
$i = 0;
foreach ($slots as $slot) { foreach ($slots as $slot) {
$rowDatetime = $slot->title; $rowDatetime = $slot->title;
$moments = explode(',', $slot->moments); $moments = explode(',', $slot->moments);
if ($datetime == $rowDatetime) { if ($datetime == $rowDatetime) {
$i += count($moments);
// Here we have to insert at the end of a slot // Here we have to insert at the end of a slot
$result->insert += count($moments);
$result->slot = $slot; $result->slot = $slot;
$result->insert = $i;
break; break;
} elseif ($datetime < $rowDatetime) { } elseif ($datetime < $rowDatetime) {
// Here we have to insert a new slot // We have to insert before this slot
break; break;
} else { } else {
$i += count($moments); $result->insert += count($moments);
} }
} }
$result->insert = $i;
return $result; return $result;
} }