1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Consumers; |
4
|
|
|
|
5
|
|
|
use Puzzle\AMQP\Consumer; |
6
|
|
|
use Swarrot\Broker\MessageProvider\PeclPackageMessageProvider; |
7
|
|
|
use Swarrot\Consumer as SwarrotConsumer; |
8
|
|
|
use Swarrot\Processor\Stack; |
9
|
|
|
use Swarrot\Processor\ProcessorInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
use Psr\Log\LoggerAwareTrait; |
12
|
|
|
use Puzzle\AMQP\Client; |
13
|
|
|
use Puzzle\AMQP\Workers\WorkerContext; |
14
|
|
|
|
15
|
|
|
abstract class AbstractConsumer implements Consumer |
16
|
|
|
{ |
17
|
|
|
use LoggerAwareTrait; |
18
|
|
|
|
19
|
|
|
protected |
20
|
|
|
$messageProvider; |
21
|
|
|
|
22
|
|
|
private |
23
|
|
|
$client, |
24
|
|
|
$processor, |
25
|
|
|
$workerContext; |
26
|
|
|
|
27
|
8 |
|
public function __construct() |
28
|
|
|
{ |
29
|
8 |
|
$this->logger = new NullLogger(); |
30
|
8 |
|
} |
31
|
|
|
|
32
|
|
|
public function consume(ProcessorInterface $processor, Client $client, WorkerContext $workerContext) |
33
|
|
|
{ |
34
|
|
|
$this->processor = $processor; |
35
|
|
|
$this->client = $client; |
36
|
|
|
$this->workerContext = $workerContext; |
37
|
|
|
$this->setMessageProvider(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function setMessageProvider() |
41
|
|
|
{ |
42
|
|
|
$this->messageProvider = new PeclPackageMessageProvider( |
43
|
|
|
$this->client->getQueue( |
44
|
|
|
$this->workerContext->getQueueName() |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getBaseStack() |
49
|
|
|
{ |
50
|
|
|
$stack = (new Stack\Builder()) |
51
|
|
|
->push('Swarrot\Processor\SignalHandler\SignalHandlerProcessor', $this->logger) |
52
|
|
|
->push('Swarrot\Processor\ExceptionCatcher\ExceptionCatcherProcessor', $this->logger) |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
return $stack; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getSwarrotConsumer(Stack\Builder $stack) |
59
|
|
|
{ |
60
|
|
|
return new SwarrotConsumer( |
61
|
|
|
$this->messageProvider, |
62
|
|
|
$stack->resolve($this->processor), |
63
|
|
|
null, |
64
|
|
|
$this->logger |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|