Skip to content

Commit 062eda2

Browse files
committed
Move filters into collection class.
1 parent d287746 commit 062eda2

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ parameters:
1414
- identifier: missingType.iterableValue
1515
- identifier: missingType.generics
1616
- identifier: trait.unused
17-
- '#Access to an undefined property Cake\\ORM\\BehaviorRegistry::\$Search#'
1817
- '#Negated boolean expression is always false.#'
1918
- '#Parameter \#1 \$.+ of function call_user_func_array expects .+, array.+ given.#'
2019

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Queue\Model\Filter;
5+
6+
use Cake\Http\Exception\NotImplementedException;
7+
use Cake\I18n\DateTime;
8+
use Cake\ORM\Query\SelectQuery;
9+
use Search\Model\Filter\FilterCollection;
10+
11+
class QueuedJobsCollection extends FilterCollection {
12+
13+
/**
14+
* @return void
15+
*/
16+
public function initialize(): void {
17+
$this
18+
->value('job_task')
19+
->like('search', [
20+
'before' => true,
21+
'after' => true,
22+
'field' => ['job_group', 'reference', 'status'],
23+
])
24+
->add('status', 'Search.Callback', [
25+
'callback' => function (SelectQuery $query, array $args, $filter) {
26+
$status = $args['status'];
27+
if ($status === 'completed') {
28+
$query->where(['completed IS NOT' => null]);
29+
30+
return true;
31+
}
32+
if ($status === 'in_progress') {
33+
$query->where([
34+
'completed IS' => null,
35+
'OR' => [
36+
'notbefore <=' => new DateTime(),
37+
'notbefore IS' => null,
38+
],
39+
]);
40+
41+
return true;
42+
}
43+
if ($status === 'scheduled') {
44+
$query->where(['completed IS' => null, 'notbefore >' => new DateTime()]);
45+
46+
return true;
47+
}
48+
49+
throw new NotImplementedException('Invalid status type');
50+
},
51+
]);
52+
}
53+
54+
}

src/Model/Table/QueuedJobsTable.php

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Cake\Core\Configure;
88
use Cake\Core\Plugin;
99
use Cake\Event\EventInterface;
10-
use Cake\Http\Exception\NotImplementedException;
1110
use Cake\I18n\DateTime;
1211
use Cake\ORM\Query\SelectQuery;
1312
use Cake\ORM\Table;
@@ -16,11 +15,11 @@
1615
use InvalidArgumentException;
1716
use Queue\Config\JobConfig;
1817
use Queue\Model\Entity\QueuedJob;
18+
use Queue\Model\Filter\QueuedJobsCollection;
1919
use Queue\Queue\Config;
2020
use Queue\Queue\TaskFinder;
2121
use Queue\Utility\Memory;
2222
use RuntimeException;
23-
use Search\Manager;
2423

2524
/**
2625
@@ -116,7 +115,9 @@ public function initialize(array $config): void {
116115

117116
$this->addBehavior('Timestamp');
118117
if (Configure::read('Queue.isSearchEnabled') !== false && Plugin::isLoaded('Search')) {
119-
$this->addBehavior('Search.Search');
118+
$this->addBehavior('Search.Search', [
119+
'collectionClass' => QueuedJobsCollection::class,
120+
]);
120121
}
121122

122123
$this->belongsTo('WorkerProcesses', [
@@ -143,46 +144,6 @@ public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObj
143144
}
144145
}
145146

146-
/**
147-
* @return \Search\Manager
148-
*/
149-
public function searchManager(): Manager {
150-
$searchManager = $this->behaviors()->Search->searchManager();
151-
$searchManager
152-
->value('job_task')
153-
->like('search', ['fields' => ['job_group', 'reference', 'status'], 'before' => true, 'after' => true])
154-
->add('status', 'Search.Callback', [
155-
'callback' => function (SelectQuery $query, array $args, $filter) {
156-
$status = $args['status'];
157-
if ($status === 'completed') {
158-
$query->where(['completed IS NOT' => null]);
159-
160-
return true;
161-
}
162-
if ($status === 'in_progress') {
163-
$query->where([
164-
'completed IS' => null,
165-
'OR' => [
166-
'notbefore <=' => new DateTime(),
167-
'notbefore IS' => null,
168-
],
169-
]);
170-
171-
return true;
172-
}
173-
if ($status === 'scheduled') {
174-
$query->where(['completed IS' => null, 'notbefore >' => new DateTime()]);
175-
176-
return true;
177-
}
178-
179-
throw new NotImplementedException('Invalid status type');
180-
},
181-
]);
182-
183-
return $searchManager;
184-
}
185-
186147
/**
187148
* Default validation rules.
188149
*

0 commit comments

Comments
 (0)