55namespace Memcrab \Queue ;
66
77use Aws \Sqs \SqsClient ;
8- use Aws \Exception \AwsException ;
98
109class 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