1 | <?php |
||
21 | class Consumer |
||
22 | { |
||
23 | /** |
||
24 | * @var AbstractConnection |
||
25 | */ |
||
26 | private $connection; |
||
27 | |||
28 | /** |
||
29 | * @var null|Qos |
||
30 | */ |
||
31 | private $qos; |
||
32 | |||
33 | /** |
||
34 | * @var Queue |
||
35 | */ |
||
36 | private $queue; |
||
37 | |||
38 | /** |
||
39 | * @var WorkerInterface |
||
40 | */ |
||
41 | private $worker; |
||
42 | |||
43 | /** |
||
44 | * @var null|Producer |
||
45 | */ |
||
46 | private $producer; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Simple fluent constructor to avoid weird-looking constructions like |
||
51 | * |
||
52 | * ```php |
||
53 | * $qos = (new Consumer())->run(); |
||
54 | * ``` |
||
55 | * |
||
56 | * @param AbstractConnection $connection |
||
57 | * @return Consumer |
||
58 | */ |
||
59 | public static function factory(AbstractConnection $connection) |
||
60 | { |
||
61 | return new static($connection); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Consumer constructor. |
||
66 | * @param AbstractConnection $connection |
||
67 | */ |
||
68 | public function __construct(AbstractConnection $connection) |
||
69 | { |
||
70 | $this->connection = $connection; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param Qos $qos |
||
75 | * @return Consumer $this |
||
76 | */ |
||
77 | public function withQos(Qos $qos) |
||
78 | { |
||
79 | $this->qos = $qos; |
||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param Queue $queue |
||
85 | * @return Consumer $this |
||
86 | */ |
||
87 | public function withQueue(Queue $queue) |
||
88 | { |
||
89 | $this->queue = $queue; |
||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param WorkerInterface $worker |
||
95 | * @return Consumer $this |
||
96 | * @throws \AmqpWorkers\Exception\ConsumerNotProperlyConfigured |
||
97 | */ |
||
98 | public function withWorker(WorkerInterface $worker) |
||
104 | |||
105 | /** |
||
106 | * If producer is set, Consumer will call `Producer::produce` with whatever Worker will return |
||
107 | * |
||
108 | * @param Producer $producer |
||
109 | * @return Consumer $this |
||
110 | */ |
||
111 | public function produceResult(Producer $producer) |
||
116 | |||
117 | /** |
||
118 | * Starts consumer. By default, this function can be terminated only by Worker's exception |
||
119 | * |
||
120 | * @throws ConsumerNotProperlyConfigured |
||
121 | * @throws ProducerNotProperlyConfigured if given producer is not properly configured |
||
122 | */ |
||
123 | public function run() |
||
172 | } |
||
173 |