Skip to content

Commit 2f068ae

Browse files
refactor lib standarts
1 parent d2d789a commit 2f068ae

File tree

3 files changed

+98
-64
lines changed

3 files changed

+98
-64
lines changed

src/Queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
interface Queue
88
{
99
public static function obj(): Queue;
10-
public function setConnectionProperties(array $properties): array;
10+
public static function setConnectionProperties(array $properties): void;
1111
public function connect(): Queue;
1212
public function registerQueue(string $name);
1313
public function sendMessage(string $name, array $messageBody);

src/RMQ.php

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
class RMQ implements Queue
1111
{
1212
private static RMQ $instance;
13-
private static $conectionProperties;
13+
private static string $host;
14+
private static int $port;
15+
private static string $username;
16+
private static string $password;
1417
private $client;
1518
private $channel;
1619

17-
public function __construct()
20+
21+
private function __construct()
1822
{
1923
}
20-
public function __clone()
24+
private function __clone()
2125
{
2226
}
2327
public function __wakeup()
@@ -36,33 +40,45 @@ public static function obj(): Queue
3640
return self::$instance;
3741
}
3842

39-
/**
40-
* @param array $properties
41-
*
42-
* @return array
43-
*/
4443
public static function setConnectionProperties(array $properties): void
4544
{
46-
self::$conectionProperties = [
47-
'host' => $properties[0],
48-
'port' => $properties[1],
49-
'username' => $properties[2],
50-
'pass' => $properties[3]
51-
];
45+
try {
46+
if (!isset($properties['host']) || empty($properties['host']) || !is_string($properties['host'])) {
47+
throw new \Exception("RabbitMQ `host` property need to be string");
48+
}
49+
if (!isset($properties['port']) || !is_int($properties['port'])) {
50+
throw new \Exception("RabbitMQ `port` property need to be int");
51+
}
52+
if (!isset($properties['username']) || empty($properties['username']) || !is_string($properties['username'])) {
53+
throw new \Exception("RabbitMQ `username` property need to be string");
54+
}
55+
if (!isset($properties['password']) || empty($properties['password']) || !is_string($properties['password'])) {
56+
throw new \Exception("RabbitMQ `password` property need to be string");
57+
}
58+
59+
self::$host = $properties['host'];
60+
self::$port = $properties['port'];
61+
self::$username = $properties['username'];
62+
self::$password = $properties['password'];
63+
} catch (\Exception $e) {
64+
error_log((string) $e);
65+
throw $e;
66+
}
5267
}
5368

5469
/**
5570
* @return Queue
5671
*/
5772
public function connect(): Queue
5873
{
59-
$client = $this->client = new AMQPStreamConnection(
60-
self::$conectionProperties['host'],
61-
self::$conectionProperties['port'],
62-
self::$conectionProperties['username'],
63-
self::$conectionProperties['pass']
74+
$this->client = new AMQPStreamConnection(
75+
self::$host,
76+
self::$port,
77+
self::$username,
78+
self::$password
6479
);
65-
$this->channel = $client->channel();
80+
81+
$this->channel = $this->client->channel();
6682

6783
return $this;
6884
}
@@ -75,15 +91,9 @@ public function connect(): Queue
7591
* @param bool $auto_delete
7692
* @return array
7793
*/
78-
public function registerQueue(
79-
string $name,
80-
bool $passive = false,
81-
bool $durable = false,
82-
bool $exclusive = false,
83-
bool $auto_delete = false
84-
): array {
94+
public function registerQueue(string $name, bool $passive = false, bool $durable = false, bool $exclusive = false, bool $auto_delete = false): array
95+
{
8596
$result = $this->channel->queue_declare($name, $passive, $durable, $exclusive, $auto_delete);
86-
8797
return $result;
8898
}
8999

@@ -96,15 +106,9 @@ public function registerQueue(
96106
*
97107
* @return [type]
98108
*/
99-
public function registerExchange(
100-
string $name,
101-
string $type,
102-
bool $passive = false,
103-
bool $durable = false,
104-
bool $auto_delete = false
105-
) {
109+
public function registerExchange(string $name, string $type, bool $passive = false, bool $durable = false, bool $auto_delete = false)
110+
{
106111
$result = $this->channel->exchange_declare($name, $type, $passive, $durable, $auto_delete);
107-
108112
return $result;
109113
}
110114

@@ -174,6 +178,9 @@ public function client(): object
174178
return $this->client;
175179
}
176180

181+
// TODO: add destructor with disconnect of connection
182+
// TODO: recheck if we realy need do both channel and client close() in shutdown or only client need to looc to documentaion of Rabbit SDK
183+
// TODO: add checks in shutdown if instance are already initialized before call close() methods as it static method and can be called it self even before any obj() initialization
177184
/**
178185
* @return void
179186
*/

src/SQS.php

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
namespace Memcrab\Queue;
66

77
use Aws\Sqs\SqsClient;
8-
use Aws\Exception\AwsException;
98

109
class SQS implements Queue
1110
{
12-
13-
private static SQS $instance;
14-
private static $config;
1511
private $client;
1612
private array $urls;
13+
private static SQS $instance;
14+
private static string $region;
15+
private static string $version;
16+
private static string $endpoint;
17+
private static string $key;
18+
private static string $secret;
1719

18-
public function __construct()
20+
private function __construct()
1921
{
2022
}
21-
public function __clone()
23+
private function __clone()
2224
{
2325
}
2426
public function __wakeup()
@@ -39,19 +41,34 @@ public static function obj(): Queue
3941
*
4042
* @return array
4143
*/
42-
public static function setConnectionProperties(array $properties): Queue
44+
public static function setConnectionProperties(array $properties): void
4345
{
44-
self::$conectionProperties = [
45-
'region' => $properties['region'],
46-
'version' => $properties['version'],
47-
'endpoint' => $properties['endpoint'];
48-
'credentials' => [
49-
'key' => $properties['key'],
50-
'secret' => $properties['secret'],
51-
]
52-
];
53-
54-
return $this;
46+
try {
47+
if (!isset($properties['region']) || empty($properties['region']) || !is_string($properties['region'])) {
48+
throw new \Exception("Aws `region` property need to be string");
49+
}
50+
if (!isset($properties['version']) || empty($properties['version']) || !is_string($properties['version'])) {
51+
throw new \Exception("Aws `version` property need to be string");
52+
}
53+
if (!isset($properties['endpoint']) || empty($properties['endpoint']) || !is_string($properties['endpoint'])) {
54+
throw new \Exception("Aws `endpoint` property need to be string");
55+
}
56+
if (!isset($properties['key']) || empty($properties['key']) || !is_string($properties['key'])) {
57+
throw new \Exception("Aws `key` property need to be string");
58+
}
59+
if (!isset($properties['secret']) || empty($properties['secret']) || !is_string($properties['secret'])) {
60+
throw new \Exception("Aws `secret` property need to be string");
61+
}
62+
63+
self::$region = $properties['region'];
64+
self::$version = $properties['version'];
65+
self::$endpoint = $properties['endpoint'];
66+
self::$key = $properties['key'];
67+
self::$secret = $properties['secret'];
68+
} catch (\Exception $e) {
69+
error_log((string) $e);
70+
throw $e;
71+
}
5572
}
5673

5774
/**
@@ -60,8 +77,18 @@ public static function setConnectionProperties(array $properties): Queue
6077
public function connect(): Queue
6178
{
6279
try {
63-
$this->client = new SqsClient(self::connectionProperties);
64-
} catch (AwsException $e) {
80+
$this->client = new SqsClient(
81+
[
82+
'region' => self::$region,
83+
'version' => self::$version,
84+
'endpoint' => self::$endpoint,
85+
'credentials' => [
86+
'key' => self::$key,
87+
'secret' => self::$secret,
88+
]
89+
]
90+
);
91+
} catch (\Exception $e) {
6592
error_log((string) $e);
6693
throw $e;
6794
}
@@ -83,7 +110,7 @@ public function registerQueue(string $name, ?array $atributes = []): Queue
83110
'Attributes' => $atributes,
84111
]);
85112
$this->urls[$name] = $result->get('QueueUrl');
86-
} catch (AwsException $e) {
113+
} catch (\Exception $e) {
87114
error_log((string) $e);
88115
throw $e;
89116
}
@@ -106,7 +133,7 @@ public function changeMessageVisibility(string $name, array $message, int $Visib
106133
'ReceiptHandle' => $message['ReceiptHandle'],
107134
'VisibilityTimeout' => $VisibilityTimeout,
108135
]);
109-
} catch (AwsException $e) {
136+
} catch (\Exception $e) {
110137
error_log((string) $e);
111138
throw $e;
112139
}
@@ -131,7 +158,7 @@ public function sendMessage(string $name, array $messageBody, ?array $attributes
131158
'MessageBody' => serialize($messageBody),
132159
'QueueUrl' => $this->urls[$name],
133160
]);
134-
} catch (AwsException $e) {
161+
} catch (\Exception $e) {
135162
error_log((string) $e);
136163
throw $e;
137164
}
@@ -152,7 +179,7 @@ public function receiveMessage(string $name)
152179
'QueueUrl' => $this->urls[$name], // REQUIRED
153180
'WaitTimeSeconds' => 20,
154181
]);
155-
} catch (AwsException $e) {
182+
} catch (\Exception $e) {
156183
error_log((string) $e);
157184
throw $e;
158185
}
@@ -171,7 +198,7 @@ public function deleteMessage(string $name, array $message)
171198
'QueueUrl' => $this->urls[$name], // REQUIRED
172199
'ReceiptHandle' => $message['ReceiptHandle'], // REQUIRED
173200
]);
174-
} catch (AwsException $e) {
201+
} catch (\Exception $e) {
175202
error_log((string) $e);
176203
throw $e;
177204
}
@@ -193,15 +220,15 @@ public function getQueueUrl(string $queueName): string
193220

194221
public static function shutdown(): void
195222
{
196-
if(isset(self::$instance->client)) {
223+
if (isset(self::$instance->client)) {
197224
self::$instance->client->destroy();
198225
unset(self::$instance->client);
199226
}
200227
}
201228

202229
public function __destruct()
203230
{
204-
if(!empty($this->client)) {
231+
if (!empty($this->client)) {
205232
$this->client->destroy();
206233
}
207234
}

0 commit comments

Comments
 (0)