| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace UniMan\Drivers\RabbitMQ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use UniMan\Core\Exception\ConnectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class RabbitMQManagementApiClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     private $baseUrl; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 | 2 |  |     public function __construct(string $host = 'localhost', string $port = '15672', string $user = 'guest', string $password = 'guest') | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 | 2 |  |         $this->baseUrl = 'http://' . $user . ':' . $password . '@' . $host . ':' . $port; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 2 |  |     public function overview(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 2 |  |         return $this->call('/api/overview'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function getVhosts(?string $user = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $result = $this->call('/api/vhosts'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $vhosts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         foreach ($result as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             if ($user === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 $vhosts[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $permissions = $this->call('/api/vhosts/' . urlencode($item['name']) . '/permissions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             foreach ($permissions as $permission) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 if ($permission['user'] === $user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     $vhosts[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return $vhosts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function getQueues(?string $vhost = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $endpoint = '/api/queues'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if ($vhost) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $endpoint .= '/' . urlencode($vhost); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $queues = $this->call($endpoint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return $queues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function getMessages(string $vhost, string $queue): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         foreach ($this->getQueues($vhost) as $vhostQueue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             if ($vhostQueue['name'] === $queue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $count = $vhostQueue['messages']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ($count === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $endpoint = '/api/queues/' . urlencode($vhost) . '/' . urlencode($queue) . '/get'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'count' => $count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'requeue' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'encoding' => 'auto', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $result = $this->call($endpoint, 'POST', json_encode($params)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |     private function call(string $endpoint, string $method = 'GET', ?array $params = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |         $ch = curl_init($this->baseUrl . $endpoint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |         if ($params) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |         curl_setopt($ch, CURLOPT_TIMEOUT, 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |         $response = curl_exec($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |         curl_close($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |         if (!$response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $result = json_decode((string)$response, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (isset($result['error'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             throw new ConnectException($result['reason']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |