AMQP::__destruct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Aimeos\MW\MQueue\Queue;
4
5
6
class AMQP implements Iface
7
{
8
	private $channel;
9
	private $queue;
10
11
12
	/**
13
	 * Initializes the message queue class
14
	 *
15
	 * @param \PhpAmqpLib\Channel\AMQPChannel $channel AMQP channel
16
	 * @param string $queue Message queue name
17
	 * @throws \Aimeos\MW\MQueue\Exception
18
	 */
19
	public function __construct( \PhpAmqpLib\Channel\AMQPChannel $channel, $queue )
20
	{
21
		try
22
		{
23
			$channel->queue_declare( $queue, false, true, false, false );
24
			$channel->basic_qos( null, 1, null );
25
		}
26
		catch( \Exception $e )
27
		{
28
			throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() );
29
		}
30
31
		$this->channel = $channel;
32
		$this->queue = $queue;
33
	}
34
35
36
	/**
37
	 * Closes the channel on cleanup
38
	 */
39
	public function __destruct()
40
	{
41
		try {
42
			$this->channel->close();
43
		} catch( \Exception $e ) { ; }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
44
	}
45
46
47
	/**
48
	 * Adds a new message to the message queue
49
	 *
50
	 * @param string $msg Message, e.g. JSON encoded data
51
	 * @throws \Aimeos\MW\MQueue\Exception
52
	 */
53
	public function add( $msg )
54
	{
55
		try
56
		{
57
			$message = new \PhpAmqpLib\Message\AMQPMessage( $msg, array( 'delivery_mode' => 2 ) );
58
			$this->channel->basic_publish( $message, '', $this->queue );
59
		}
60
		catch( \Exception $e )
61
		{
62
			throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() );
63
		}
64
	}
65
66
67
	/**
68
	 * Removes the message from the queue
69
	 *
70
	 * @param \Aimeos\MW\MQueue\Message\Iface $msg Message object
71
	 * @throws \Aimeos\MW\MQueue\Exception
72
	 */
73
	public function del( \Aimeos\MW\MQueue\Message\Iface $msg )
74
	{
75
		try {
76
			$this->channel->basic_ack( $msg->getObject()->delivery_info['delivery_tag'] );
77
		} catch( \Exception $e ) {
78
			throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() );
79
		}
80
	}
81
82
83
	/**
84
	 * Returns the next message from the queue
85
	 *
86
	 * @return \Aimeos\MW\MQueue\Message\Iface|null Message object or null if none is available
87
	 * @throws \Aimeos\MW\MQueue\Exception
88
	 */
89
	public function get()
90
	{
91
		try
92
		{
93
			if( ( $msg = $this->channel->basic_get( $this->queue ) ) !== null ) {
94
				return new \Aimeos\MW\MQueue\Message\AMQP( $msg );
95
			}
96
		}
97
		catch( \Exception $e )
98
		{
99
			throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() );
100
		}
101
	}
102
}
103