Skip to content

Commit 852e093

Browse files
committed
Refactory Queue::ensureGetIndex()
1 parent 988ab4a commit 852e093

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/Queue.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,28 @@ 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-
foreach ($beforeSort as $key => $value) {
77-
if (!is_string($key)) {
78-
throw new \InvalidArgumentException('key in $beforeSort was not a string');
79-
}
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+
}
8081

81-
if ($value !== 1 && $value !== -1) {
82-
throw new \InvalidArgumentException('value of $beforeSort is not 1 or -1 for ascending and descending');
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;
8389
}
90+
};
8491

85-
$completeFields["payload.{$key}"] = $value;
86-
}
92+
$verifySort($beforeSort, 'beforeSort', $completeFields);
8793

8894
$completeFields['priority'] = 1;
8995
$completeFields['created'] = 1;
9096

91-
foreach ($afterSort as $key => $value) {
92-
if (!is_string($key)) {
93-
throw new \InvalidArgumentException('key in $afterSort was not a string');
94-
}
95-
96-
if ($value !== 1 && $value !== -1) {
97-
throw new \InvalidArgumentException('value of $afterSort is not 1 or -1 for ascending and descending');
98-
}
99-
100-
$completeFields["payload.{$key}"] = $value;
101-
}
97+
$verifySort($afterSort, 'afterSort', $completeFields);
10298

10399
$completeFields['earliestGet'] = 1;
104100

0 commit comments

Comments
 (0)