Beanstalk::connect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Aimeos\MW\MQueue;
4
5
6
class Beanstalk extends Base implements Iface
7
{
8
	private $client;
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', \Pheanstalk\PheanstalkInterface::DEFAULT_PORT );
23
24
		if( is_array( $host ) )
25
		{
26
			foreach( $host as $idx => $entry )
27
			{
28
				$iport = ( is_array( $port) ? $port[$idx] : $port );
29
				$this->client = $this->connect( $entry, $iport );
30
31
				if( $this->client instanceof \Pheanstalk\PheanstalkInterface ) {
0 ignored issues
show
Bug introduced by
The class Pheanstalk\PheanstalkInterface 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...
32
					break;
33
				}
34
			}
35
		}
36
		else
37
		{
38
			$this->client = $this->connect( $host, $port );
39
		}
40
41
		if( $this->client instanceof \Pheanstalk\Exception ) {
0 ignored issues
show
Bug introduced by
The class Pheanstalk\Exception 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...
42
			throw new \Aimeos\MW\MQueue\Exception( $this->client->getMessage() );
43
		}
44
	}
45
46
47
	/**
48
	 * Returns the queue for the given name
49
	 *
50
	 * @param string $name Queue name
51
	 * @return \Aimeos\MW\MQueue\Queue\Iface Message queue
52
	 */
53
	public function getQueue( $name )
54
	{
55
		if( !isset( $this->queues[$name] ) )
56
		{
57
			$timeout = $this->getConfig( 'readtimeout', 30 );
58
			$this->queues[$name] = new \Aimeos\MW\MQueue\Queue\Beanstalk( $this->client, $name, $timeout );
59
		}
60
61
		return $this->queues[$name];
62
	}
63
64
65
	/**
66
	 * Opens a connection to the message queue server
67
	 *
68
	 * @param string $host Host name or IP address
69
	 * @param integer $port Port the server is listening
70
	 * @return \Pheanstalk\PheanstalkInterface|\Pheanstalk\Exception
71
	 */
72
	protected function connect( $host, $port )
73
	{
74
		$conntimeout = $this->getConfig( 'conntimeout', 3 );
75
		$persist = $this->getConfig( 'persist', false );
76
77
		return new \Pheanstalk\Pheanstalk( $host, $port, $conntimeout, $persist );
78
	}
79
}