|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Burrow\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Burrow\Driver; |
|
6
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
|
7
|
|
|
use PhpAmqpLib\Connection\AMQPLazyConnection; |
|
8
|
|
|
|
|
9
|
|
|
class DriverFactory |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Return the driver for your system |
|
13
|
|
|
* |
|
14
|
|
|
* It accepts a PECL connection, a PhpAmqpLib connection or an array following |
|
15
|
|
|
* the following schema : |
|
16
|
|
|
* [ |
|
17
|
|
|
* 'host' => '<hostValue>', |
|
18
|
|
|
* 'port' => '<portValue>', |
|
19
|
|
|
* 'user' => '<userValue>', |
|
20
|
|
|
* 'pwd' => '<pwdValue>' |
|
21
|
|
|
* ] |
|
22
|
|
|
* |
|
23
|
|
|
* If you provide an array, the PECL extension has precedence over the PhpAmqpLib. |
|
24
|
|
|
* |
|
25
|
|
|
* @param $connection |
|
26
|
|
|
* |
|
27
|
|
|
* @return Driver |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function getDriver($connection) |
|
30
|
|
|
{ |
|
31
|
|
|
if (is_array($connection) && |
|
32
|
|
|
isset($connection['host']) && |
|
33
|
|
|
isset($connection['port']) && |
|
34
|
|
|
isset($connection['user']) && |
|
35
|
|
|
isset($connection['pwd']) |
|
36
|
|
|
) { |
|
37
|
|
|
$connection = self::getConnectionFromArray($connection); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($connection instanceof AbstractConnection) { |
|
41
|
|
|
return new PhpAmqpLibDriver($connection); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($connection instanceof \AMQPConnection) { |
|
45
|
|
|
return new PeclAmqpDriver($connection); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
throw new \InvalidArgumentException('You provided an unsupported connection'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $connection |
|
53
|
|
|
* |
|
54
|
|
|
* @return \AMQPConnection | AMQPLazyConnection |
|
55
|
|
|
*/ |
|
56
|
|
|
private static function getConnectionFromArray($connection) |
|
57
|
|
|
{ |
|
58
|
|
|
if (static::peclExtensionIsAvailable()) { |
|
59
|
|
|
return static::getPeclConnection($connection); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return static::getPhpAmqpLibConnection($connection); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string[] $connection |
|
67
|
|
|
* |
|
68
|
|
|
* @return \AMQPConnection |
|
69
|
|
|
* |
|
70
|
|
|
* @codeCoverageIgnore |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static function getPeclConnection(array $connection) |
|
73
|
|
|
{ |
|
74
|
|
|
$amqpConnection = new \AMQPConnection(); |
|
75
|
|
|
$amqpConnection->setHost($connection['host']); |
|
76
|
|
|
$amqpConnection->setPort($connection['port']); |
|
77
|
|
|
$amqpConnection->setLogin($connection['user']); |
|
78
|
|
|
$amqpConnection->setPassword($connection['pwd']); |
|
79
|
|
|
|
|
80
|
|
|
return $amqpConnection; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string[] $connection |
|
85
|
|
|
* |
|
86
|
|
|
* @return AMQPLazyConnection |
|
87
|
|
|
* |
|
88
|
|
|
* @codeCoverageIgnore |
|
89
|
|
|
*/ |
|
90
|
|
|
protected static function getPhpAmqpLibConnection(array $connection) |
|
91
|
|
|
{ |
|
92
|
|
|
return new AMQPLazyConnection( |
|
93
|
|
|
$connection['host'], |
|
94
|
|
|
$connection['port'], |
|
95
|
|
|
$connection['user'], |
|
96
|
|
|
$connection['pwd'] |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return bool |
|
102
|
|
|
* |
|
103
|
|
|
* @codeCoverageIgnore |
|
104
|
|
|
*/ |
|
105
|
|
|
protected static function peclExtensionIsAvailable() |
|
106
|
|
|
{ |
|
107
|
|
|
return class_exists(\AMQPConnection::class); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.