Skip to content

Commit c7bfd84

Browse files
committed
Merge pull request #41 from chadicus/refactor
Attempting to Increase Code Quality
2 parents 8e634b2 + a8d2bfa commit c7bfd84

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

src/Queue.php

Lines changed: 33 additions & 39 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
}
@@ -344,11 +318,8 @@ public function ackSend(array $message, array $payload, $earliestGet = 0, $prior
344318
throw new \InvalidArgumentException('$newTimestamp was not a bool');
345319
}
346320

347-
if ($earliestGet > self::MONGO_INT32_MAX) {
348-
$earliestGet = self::MONGO_INT32_MAX;
349-
} elseif ($earliestGet < 0) {
350-
$earliestGet = 0;
351-
}
321+
//Ensure $earliestGet is between 0 and MONGO_INT32_MAX
322+
$earliestGet = min(max(0, $earliestGet), self::MONGO_INT32_MAX);
352323

353324
$toSet = [
354325
'payload' => $payload,
@@ -416,11 +387,8 @@ public function send(array $payload, $earliestGet = 0, $priority = 0.0)
416387
throw new \InvalidArgumentException('$priority was NaN');
417388
}
418389

419-
if ($earliestGet > self::MONGO_INT32_MAX) {
420-
$earliestGet = self::MONGO_INT32_MAX;
421-
} elseif ($earliestGet < 0) {
422-
$earliestGet = 0;
423-
}
390+
//Ensure $earliestGet is between 0 and MONGO_INT32_MAX
391+
$earliestGet = min(max(0, $earliestGet), self::MONGO_INT32_MAX);
424392

425393
$message = [
426394
'payload' => $payload,
@@ -476,4 +444,30 @@ private function ensureIndex(array $index)
476444

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

0 commit comments

Comments
 (0)