Skip to content

Commit 3ece7e8

Browse files
committed
Reduce duplication by reusing verifySort function
1 parent 8e634b2 commit 3ece7e8

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/Queue.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,12 @@ public function ensureGetIndex(array $beforeSort = [], array $afterSort = [])
7373
//using general rule: equality, sort, range or more equality tests in that order for index
7474
$completeFields = ['running' => 1];
7575

76-
$verifySort = function ($sort, $label, &$completeFields) {
77-
foreach ($sort as $key => $value) {
78-
if (!is_string($key)) {
79-
throw new \InvalidArgumentException("key in \${$label} was not a string");
80-
}
81-
82-
if ($value !== 1 && $value !== -1) {
83-
throw new \InvalidArgumentException(
84-
'value of \${$label} is not 1 or -1 for ascending and descending'
85-
);
86-
}
87-
88-
$completeFields["payload.{$key}"] = $value;
89-
}
90-
};
91-
92-
$verifySort($beforeSort, 'beforeSort', $completeFields);
76+
self::verifySort($beforeSort, 'beforeSort', $completeFields);
9377

9478
$completeFields['priority'] = 1;
9579
$completeFields['created'] = 1;
9680

97-
$verifySort($afterSort, 'afterSort', $completeFields);
81+
self::verifySort($afterSort, 'afterSort', $completeFields);
9882

9983
$completeFields['earliestGet'] = 1;
10084

@@ -131,17 +115,7 @@ public function ensureCountIndex(array $fields, $includeRunning)
131115
$completeFields['running'] = 1;
132116
}
133117

134-
foreach ($fields as $key => $value) {
135-
if (!is_string($key)) {
136-
throw new \InvalidArgumentException('key in $fields was not a string');
137-
}
138-
139-
if ($value !== 1 && $value !== -1) {
140-
throw new \InvalidArgumentException('value of $fields is not 1 or -1 for ascending and descending');
141-
}
142-
143-
$completeFields["payload.{$key}"] = $value;
144-
}
118+
self::verifySort($fields, 'fields', $completeFields);
145119

146120
$this->ensureIndex($completeFields);
147121
}
@@ -476,4 +450,30 @@ private function ensureIndex(array $index)
476450

477451
throw new \Exception('couldnt create index after 5 attempts');
478452
}
453+
454+
/**
455+
* Helper method to validate keys and values for the given sort array
456+
*
457+
* @param array $sort The proposed sort for a mongo index.
458+
* @param string $label The name of the variable given to the public ensureXIndex method.
459+
* @param array &$completedFields The final index array with payload. prefix added to fields.
460+
*
461+
* @return void
462+
*/
463+
private static function verifySort(array $sort, $label, &$completeFields)
464+
{
465+
foreach ($sort as $key => $value) {
466+
if (!is_string($key)) {
467+
throw new \InvalidArgumentException("key in \${$label} was not a string");
468+
}
469+
470+
if ($value !== 1 && $value !== -1) {
471+
throw new \InvalidArgumentException(
472+
"value of \${$label} is not 1 or -1 for ascending and descending"
473+
);
474+
}
475+
476+
$completeFields["payload.{$key}"] = $value;
477+
}
478+
}
479479
}

0 commit comments

Comments
 (0)