This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Drupal\rabbitmq; |
||
4 | |||
5 | use Drupal\Core\Site\Settings; |
||
6 | use PhpAmqpLib\Connection\AMQPStreamConnection; |
||
7 | use PhpAmqpLib\Connection\AMQPSSLConnection; |
||
8 | |||
9 | /** |
||
10 | * RabbitMQ connection factory class. |
||
11 | */ |
||
12 | class ConnectionFactory { |
||
13 | const DEFAULT_CREDENTIALS_KEY = 'default'; |
||
14 | const DEFAULT_SERVER_ALIAS = 'localhost'; |
||
15 | const DEFAULT_HOST = self::DEFAULT_SERVER_ALIAS; |
||
16 | const DEFAULT_PORT = 5672; |
||
17 | const DEFAULT_USER = 'guest'; |
||
18 | const DEFAULT_PASS = 'guest'; |
||
19 | |||
20 | const CREDENTIALS = 'rabbitmq_credentials'; |
||
21 | |||
22 | /** |
||
23 | * The RabbitMQ connection. |
||
24 | * |
||
25 | * @var \PhpAmqpLib\Connection\AMQPStreamConnection |
||
26 | */ |
||
27 | protected $connection; |
||
28 | |||
29 | /** |
||
30 | * The settings service. |
||
31 | * |
||
32 | * @var \Drupal\Core\Site\Settings |
||
33 | */ |
||
34 | protected $settings; |
||
35 | |||
36 | /** |
||
37 | * The name of the server to connect to. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | 5 | protected $credentialsKey; |
|
42 | |||
43 | 5 | /** |
|
44 | 5 | * Constructor. |
|
45 | * |
||
46 | 5 | * @param \Drupal\Core\Site\Settings $settings |
|
47 | 5 | * The settings service. |
|
48 | * @param string $credentialsKey |
||
49 | * The key of the credentials to use for the connection. |
||
50 | */ |
||
51 | public function __construct(Settings $settings, $credentialsKey = 'default') { |
||
52 | // Cannot continue if the library wasn't loaded. |
||
53 | assert(class_exists('\PhpAmqpLib\Connection\AMQPStreamConnection'), |
||
54 | 'Could not find php-amqplib. See the rabbitmq/README.md file for details.' |
||
55 | 5 | ); |
|
56 | 5 | $this->settings = $settings; |
|
57 | $this->credentialsKey = $credentialsKey; |
||
58 | 5 | } |
|
59 | 5 | ||
60 | 5 | /** |
|
61 | 5 | * Get a configured connection to RabbitMQ. |
|
62 | 5 | * |
|
63 | * @return \PhpAmqpLib\Connection\AMQPSSLConnection|\PhpAmqpLib\Connection\AMQPStreamConnection |
||
64 | * The AMQP or SSL connection. |
||
65 | 5 | */ |
|
66 | public function getConnection() { |
||
67 | 5 | if (empty($this->connection)) { |
|
68 | if (!empty($credentials['ssl'])) { |
||
0 ignored issues
–
show
|
|||
69 | $connection = $this->getSecureConnection(); |
||
70 | } |
||
71 | else { |
||
72 | $connection = $this->getStandardConnection(); |
||
73 | } |
||
74 | |||
75 | $this->connection = $connection; |
||
76 | } |
||
77 | |||
78 | return $this->connection; |
||
79 | 5 | } |
|
80 | 5 | ||
81 | 5 | /** |
|
82 | 5 | * Get the credentials defined in the settings.php file. |
|
83 | 5 | * |
|
84 | 5 | * @return array |
|
85 | * Array of credentials. |
||
86 | */ |
||
87 | 5 | protected function getCredentials() { |
|
88 | $defaultCredentials['default'] = [ |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$defaultCredentials was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultCredentials = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
89 | 5 | 'host' => static::DEFAULT_SERVER_ALIAS, |
|
90 | 'port' => static::DEFAULT_PORT, |
||
91 | 'username' => static::DEFAULT_USER, |
||
92 | 'password' => static::DEFAULT_PASS, |
||
93 | 'vhost' => '/', |
||
94 | ]; |
||
95 | |||
96 | $credentials = $this->settings->get(self::CREDENTIALS, $defaultCredentials); |
||
97 | |||
98 | if (!array_key_exists($this->credentialsKey, $credentials)) { |
||
99 | $this->credentialsKey = static::DEFAULT_CREDENTIALS_KEY; |
||
100 | } |
||
101 | |||
102 | return $credentials[$this->credentialsKey]; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return SSL connection object. |
||
107 | * |
||
108 | * @return \PhpAmqpLib\Connection\AMQPSSLConnection |
||
109 | * SSL connection object. |
||
110 | */ |
||
111 | protected function getSecureConnection() { |
||
112 | $credentials = $this->getCredentials(); |
||
113 | return new AMQPSSLConnection( |
||
114 | $credentials['host'], |
||
115 | $credentials['port'], |
||
116 | $credentials['username'], |
||
117 | $credentials['password'], |
||
118 | $credentials['vhost'], |
||
119 | $credentials['ssl'], |
||
120 | $credentials['options'] |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Return standard connection object. |
||
126 | * |
||
127 | * @return \PhpAmqpLib\Connection\AMQPStreamConnection |
||
128 | * Standard connection object. |
||
129 | */ |
||
130 | protected function getStandardConnection() { |
||
131 | $credentials = $this->getCredentials(); |
||
132 | $defaultOptions = [ |
||
133 | 'insist' => FALSE, |
||
134 | 'login_method' => 'AMQPLAIN', |
||
135 | 'login_response' => NULL, |
||
136 | 'locale' => 'en_US', |
||
137 | 'connection_timeout' => 3.0, |
||
138 | 'read_write_timeout' => 3.0, |
||
139 | 'context' => NULL, |
||
140 | 'keepalive' => FALSE, |
||
141 | 'heartbeat' => 0, |
||
142 | ]; |
||
143 | |||
144 | if (empty($credentials['options'])) { |
||
145 | $credentials['options'] = []; |
||
146 | } |
||
147 | |||
148 | $credentials['options'] = array_merge($defaultOptions, $credentials['options']); |
||
149 | |||
150 | return new AMQPStreamConnection( |
||
151 | $credentials['host'], |
||
152 | $credentials['port'], |
||
153 | $credentials['username'], |
||
154 | $credentials['password'], |
||
155 | $credentials['vhost'], |
||
156 | $credentials['options']['insist'], |
||
157 | $credentials['options']['login_method'], |
||
158 | $credentials['options']['login_response'], |
||
159 | $credentials['options']['locale'], |
||
160 | $credentials['options']['connection_timeout'], |
||
161 | $credentials['options']['read_write_timeout'], |
||
162 | $credentials['options']['context'], |
||
163 | $credentials['options']['keepalive'], |
||
164 | $credentials['options']['heartbeat'] |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | } |
||
169 |
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.