@@ -25,16 +25,21 @@ final class Queue implements QueueInterface
2525 /**
2626 * Construct queue.
2727 *
28- * @param string $url the mongo url
28+ * @param \MongoCollection| string $collectionOrUrl A MongoCollection instance or the mongo connection url.
2929 * @param string $db the mongo db name
3030 * @param string $collection the collection name to use for the queue
3131 *
32- * @throws \InvalidArgumentException $url , $db or $collection was not a string
32+ * @throws \InvalidArgumentException $collectionOrUrl , $db or $collection was not a string
3333 */
34- public function __construct ($ url , $ db , $ collection )
34+ public function __construct ($ collectionOrUrl , $ db = null , $ collection = null )
3535 {
36- if (!is_string ($ url )) {
37- throw new \InvalidArgumentException ('$url was not a string ' );
36+ if ($ collectionOrUrl instanceof \MongoCollection) {
37+ $ this ->collection = $ collectionOrUrl ;
38+ return ;
39+ }
40+
41+ if (!is_string ($ collectionOrUrl )) {
42+ throw new \InvalidArgumentException ('$collectionOrUrl was not a string ' );
3843 }
3944
4045 if (!is_string ($ db )) {
@@ -45,7 +50,7 @@ public function __construct($url, $db, $collection)
4550 throw new \InvalidArgumentException ('$collection was not a string ' );
4651 }
4752
48- $ mongo = new \MongoClient ($ url );
53+ $ mongo = new \MongoClient ($ collectionOrUrl );
4954 $ mongoDb = $ mongo ->selectDB ($ db );
5055 $ this ->collection = $ mongoDb ->selectCollection ($ collection );
5156 }
@@ -68,32 +73,28 @@ public function ensureGetIndex(array $beforeSort = [], array $afterSort = [])
6873 //using general rule: equality, sort, range or more equality tests in that order for index
6974 $ completeFields = ['running ' => 1 ];
7075
71- foreach ($ beforeSort as $ key => $ value ) {
72- if (!is_string ($ key )) {
73- throw new \InvalidArgumentException ('key in $beforeSort was not a string ' );
74- }
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+ }
7581
76- if ($ value !== 1 && $ value !== -1 ) {
77- 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 ;
7889 }
90+ };
7991
80- $ completeFields ["payload. {$ key }" ] = $ value ;
81- }
92+ $ verifySort ($ beforeSort , 'beforeSort ' , $ completeFields );
8293
8394 $ completeFields ['priority ' ] = 1 ;
8495 $ completeFields ['created ' ] = 1 ;
8596
86- foreach ($ afterSort as $ key => $ value ) {
87- if (!is_string ($ key )) {
88- throw new \InvalidArgumentException ('key in $afterSort was not a string ' );
89- }
90-
91- if ($ value !== 1 && $ value !== -1 ) {
92- throw new \InvalidArgumentException ('value of $afterSort is not 1 or -1 for ascending and descending ' );
93- }
94-
95- $ completeFields ["payload. {$ key }" ] = $ value ;
96- }
97+ $ verifySort ($ afterSort , 'afterSort ' , $ completeFields );
9798
9899 $ completeFields ['earliestGet ' ] = 1 ;
99100
0 commit comments