Total Complexity | 7 |
Total Lines | 55 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
9 | class TaskQueue |
||
10 | { |
||
11 | private $connection; |
||
12 | private $channel; |
||
13 | |||
14 | public function __construct() |
||
15 | { |
||
16 | $this->connection = $this->getConnection(); |
||
17 | $this->channel = $this->queueChannel(); |
||
18 | } |
||
19 | |||
20 | public function add(Task $task) |
||
21 | { |
||
22 | try { |
||
23 | $message = $this->makeMessage($task); |
||
24 | $this->channel->basic_publish($message, '', config('rabbitmq.routing_key')); |
||
25 | } catch (AMQPRuntimeException $e) { |
||
26 | app('log')->error('add task queue failed!', $e->getMessage()); |
||
|
|||
27 | } |
||
28 | |||
29 | } |
||
30 | |||
31 | public function done() |
||
32 | { |
||
33 | $this->channel->close(); |
||
34 | $this->connection->close(); |
||
35 | } |
||
36 | |||
37 | private function getConnection() |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | private function queueChannel() |
||
49 | { |
||
50 | $channel = $this->connection->channel(); |
||
51 | $channel->queue_declare(config('rabbitmq.queue'), false, true); |
||
52 | |||
53 | return $channel; |
||
54 | } |
||
55 | |||
56 | private function makeMessage(Task $task) |
||
64 | } |
||
65 | } |
||
66 |