|
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\DispatcherInterface; |
|
11
|
|
|
use Resque\Interfaces\SerializerInterface; |
|
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
|
|
|
public function __construct(SerializerInterface $serializer, Datastore $datastore) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->serializer = $serializer; |
|
24
|
|
|
$this->datastore = $datastore; |
|
25
|
|
|
$this->dispatcher = new Noop(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setDispatcher(DispatcherInterface $dispatcher): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->dispatcher = $dispatcher; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function enqueue(string $className, array $arguments, string $queueName = ''): void |
|
34
|
|
|
{ |
|
35
|
|
|
$payload = ['class' => $className, 'args' => $arguments]; |
|
36
|
|
|
$this->validateEnqueue($className, $queueName); |
|
37
|
|
|
|
|
38
|
|
|
$payload = $this->dispatcher->dispatch(BeforeEnqueue::class, $payload); |
|
39
|
|
|
$this->push($queueName, $payload); |
|
40
|
|
|
$this->dispatcher->dispatch(AfterEnqueue::class, $payload); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function validateEnqueue(string $className, string $queueName): void |
|
44
|
|
|
{ |
|
45
|
|
|
if (empty($queueName)) { |
|
46
|
|
|
throw new QueueMissing(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (empty($className)) { |
|
50
|
|
|
throw new JobClassMissing(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function push(string $queueName, array $payload): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->datastore->pushToQueue($queueName, $this->serializer->serialize($payload)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|