AMQP::__construct()   C
last analyzed

Complexity

Conditions 8
Paths 36

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 33
rs 5.3846
cc 8
eloc 18
nc 36
nop 1
1
<?php
2
3
namespace Aimeos\MW\MQueue;
4
5
6
class AMQP extends Base implements Iface
7
{
8
	private $conn;
9
	private $queues = array();
10
11
12
	/**
13
	 * Initializes the message queue object
14
	 *
15
	 * @param array $config Associative list of configuration key/value pairs
16
	 */
17
	public function __construct( array $config )
18
	{
19
		parent::__construct( $config );
20
21
		$host = $this->getConfig( 'host', 'localhost' );
22
		$port = $this->getConfig( 'port', 5672 );
23
		$user = $this->getConfig( 'username', 'guest' );
24
		$pass = $this->getConfig( 'password', 'guest' );
25
26
		if( is_array( $host ) )
27
		{
28
			foreach( $host as $idx => $entry )
29
			{
30
				$iport = ( is_array( $port) ? $port[$idx] : $port );
31
				$iuser = ( is_array( $user) ? $user[$idx] : $user );
32
				$ipass = ( is_array( $pass) ? $pass[$idx] : $pass );
33
34
				$this->conn = $this->connect( $entry, $iport, $iuser, $ipass );
35
36
				if( $this->conn instanceof \PhpAmqpLib\Connection\AMQPStreamConnection ) {
0 ignored issues
show
Bug introduced by
The class PhpAmqpLib\Connection\AMQPStreamConnection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37
					break;
38
				}
39
			}
40
		}
41
		else
42
		{
43
			$this->conn = $this->connect( $host, $port, $user, $pass );
44
		}
45
46
		if( $this->conn instanceof \Exception ) {
47
			throw new \Aimeos\MW\MQueue\Exception( $this->conn->getMessage() );
48
		}
49
	}
50
51
52
	/**
53
	 * Closes the open connection
54
	 */
55
	public function __destruct()
56
	{
57
		$this->conn->close();
58
	}
59
60
61
	/**
62
	 * Returns the queue for the given name
63
	 *
64
	 * @param string $name Queue name
65
	 * @return \Aimeos\MW\MQueue\Queue\Iface Message queue
66
	 */
67
	public function getQueue( $name )
68
	{
69
		try
70
		{
71
			if( !isset( $this->queues[$name] ) ) {
72
				$this->queues[$name] = new \Aimeos\MW\MQueue\Queue\AMQP( $this->getChannel(), $name );
73
			}
74
75
			return $this->queues[$name];
76
		}
77
		catch( \Exception $e )
78
		{
79
			throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() );
80
		}
81
	}
82
83
84
	/**
85
	 * Opens a connection to the message queue server
86
	 *
87
	 * @param string $host Host name or IP address
88
	 * @param integer $port Port the server is listening
89
	 * @param string $user User name for authentication
90
	 * @param string $pass Password for authentication
91
	 * @return \PhpAmqpLib\Connection\AMQPStreamConnection|\PhpAmqpLib\Exception\AMQPException
92
	 */
93
	protected function connect( $host, $port, $user, $pass )
94
	{
95
		$vhost = $this->getConfig( 'vhost', '/' );
96
		$insist = $this->getConfig( 'insist', false );
97
		$loginMethod = $this->getConfig( 'login_method', 'AMQPLAIN' );
98
		$loginResponse = $this->getConfig( 'login_response', null );
99
		$locale = $this->getConfig( 'locale', 'en_US' );
100
		$conntimeout = $this->getConfig( 'connection_timeout', 3.0 );
101
		$timeout = $this->getConfig( 'read_write_timeout', 3.0 );
102
		$keepalive = $this->getConfig( 'keepalive', false );
103
		$heartbeat = $this->getConfig( 'heartbeat', 0 );
104
105
		try
106
		{
107
			return new \PhpAmqpLib\Connection\AMQPStreamConnection(
108
				$host, $port, $user, $pass,
109
				$vhost, $insist, $loginMethod, $loginResponse,
110
				$locale, $conntimeout, $timeout, null,
111
				$keepalive, $heartbeat
112
			);
113
		}
114
		catch( \Exception $e )
115
		{
116
			return $e;
117
		}
118
	}
119
120
121
	/**
122
	 * Returns a new AMQP channel
123
	 *
124
	 * @return \PhpAmqpLib\Connection\AMQPChannel AMQP channel
125
	 */
126
	protected function getChannel()
127
	{
128
		return $this->conn->channel();
129
	}
130
}
131