1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Resque; |
6
|
|
|
|
7
|
|
|
use Resque\Dispatchers\Noop; |
8
|
|
|
use Resque\Exceptions\JobClassMissing; |
9
|
|
|
use Resque\Exceptions\QueueMissing; |
10
|
|
|
use Resque\Interfaces\Dispatcher; |
11
|
|
|
use Resque\Interfaces\Serializer; |
12
|
|
|
use Resque\Tasks\AfterEnqueue; |
13
|
|
|
use Resque\Tasks\BeforeEnqueue; |
14
|
|
|
|
15
|
|
|
class Resque |
16
|
|
|
{ |
17
|
|
|
private $dispatcher; |
18
|
|
|
private $serializer; |
19
|
|
|
private $datastore; |
20
|
|
|
|
21
|
4 |
|
public function __construct(Datastore $datastore, Serializer $serializer) |
22
|
|
|
{ |
23
|
4 |
|
$this->serializer = $serializer; |
24
|
4 |
|
$this->datastore = $datastore; |
25
|
4 |
|
$this->dispatcher = new Noop(); |
26
|
4 |
|
} |
27
|
|
|
|
28
|
4 |
|
public function setDispatcher(Dispatcher $dispatcher): void |
29
|
|
|
{ |
30
|
4 |
|
$this->dispatcher = $dispatcher; |
31
|
4 |
|
} |
32
|
|
|
|
33
|
4 |
|
public function enqueue(string $className, array $arguments, string $queueName = ''): void |
34
|
|
|
{ |
35
|
4 |
|
$payload = ['class' => $className, 'args' => $arguments, 'queue_name' => $queueName]; |
36
|
4 |
|
$this->validateEnqueue($className, $queueName); |
37
|
|
|
|
38
|
2 |
|
$payload = $this->dispatcher->dispatch(BeforeEnqueue::class, $payload); |
39
|
2 |
|
if (empty($payload['skip_queue'])) { |
40
|
1 |
|
$this->push($payload['queue_name'], $payload); |
41
|
|
|
} |
42
|
2 |
|
$this->dispatcher->dispatch(AfterEnqueue::class, $payload); |
43
|
2 |
|
} |
44
|
|
|
|
45
|
4 |
|
private function validateEnqueue(string $className, string $queueName): void |
46
|
|
|
{ |
47
|
4 |
|
if (empty($queueName)) { |
48
|
1 |
|
throw new QueueMissing(); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
if (empty($className)) { |
52
|
1 |
|
throw new JobClassMissing(); |
53
|
|
|
} |
54
|
2 |
|
} |
55
|
|
|
|
56
|
1 |
|
private function push(string $queueName, array $payload): void |
57
|
|
|
{ |
58
|
1 |
|
$this->datastore->pushToQueue($queueName, $this->serializer->serialize($payload)); |
59
|
1 |
|
} |
60
|
|
|
} |
61
|
|
|
|