|
| 1 | +<?php namespace App\Console\Commands; |
| 2 | +/* |
| 3 | + * Copyright 2025 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | +use App\Jobs\Payments\EventTypes; |
| 15 | +use Exception; |
| 16 | +use Illuminate\Console\Command; |
| 17 | +use Illuminate\Support\Facades\Config; |
| 18 | +use Illuminate\Support\Facades\Log; |
| 19 | +use PhpAmqpLib\Connection\AMQPStreamConnection; |
| 20 | + |
| 21 | +/** |
| 22 | + * Class SetupSponsorServiceMessageBrokerCommand |
| 23 | + * @package App\Console\Commands |
| 24 | + */ |
| 25 | +final class SetupPaymentServiceMessageBrokerCommand extends Command |
| 26 | +{ |
| 27 | + /** |
| 28 | + * The console command name. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $name = "setup_payment_service_message_broker"; |
| 33 | + |
| 34 | + /** |
| 35 | + * The name and signature of the console command. |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $signature = "mq:setup_payment_service_message_broker {exchange_name} {exchange_type}"; |
| 40 | + |
| 41 | + /** |
| 42 | + * The console command description. |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + protected $description = "Set up Payment Service rabbitmq exchange, queue and bindings"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Execute the console command. |
| 50 | + * |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function handle(): void |
| 54 | + { |
| 55 | + $host_settings_path = "queue.connections.message_broker.hosts.0"; |
| 56 | + $queue_settings_path = "queue.connections.payments_sync_consumer"; |
| 57 | + $host_settings = Config::get($host_settings_path); |
| 58 | + |
| 59 | + $exchange_name = $this->argument('exchange_name'); |
| 60 | + |
| 61 | + if (empty($exchange_name)) |
| 62 | + throw new \InvalidArgumentException("exchange_name is required"); |
| 63 | + |
| 64 | + $exchange_type = $this->argument('exchange_type'); |
| 65 | + |
| 66 | + if (empty($exchange_type)) |
| 67 | + throw new \InvalidArgumentException("exchange_type is required"); |
| 68 | + |
| 69 | + if (!$host_settings) { |
| 70 | + throw new \InvalidArgumentException("Host setting not found at {$host_settings_path}"); |
| 71 | + } |
| 72 | + $queue_settings = Config::get($queue_settings_path); |
| 73 | + |
| 74 | + $host = $host_settings['host']; |
| 75 | + $port = $host_settings['port']; |
| 76 | + $user = $host_settings['user']; |
| 77 | + $password = $host_settings['password']; |
| 78 | + $vhost = $host_settings['vhost']; |
| 79 | + |
| 80 | + $queue = $queue_settings['queue']; |
| 81 | + |
| 82 | + $routingKeys = [ |
| 83 | + EventTypes::PAYMENT_PROFILE_UPDATED, |
| 84 | + EventTypes::PAYMENT_PROFILE_CREATED, |
| 85 | + EventTypes::PAYMENT_PROFILE_DELETED, |
| 86 | + ]; |
| 87 | + |
| 88 | + try { |
| 89 | + $connection = new AMQPStreamConnection($host, $port, $user, $password, $vhost); |
| 90 | + $channel = $connection->channel(); |
| 91 | + |
| 92 | + // Exchange |
| 93 | + $channel->exchange_declare($exchange_name, $exchange_type, false, true, false); |
| 94 | + |
| 95 | + // Queue |
| 96 | + $channel->queue_declare($queue, false, true, false, false); |
| 97 | + |
| 98 | + // Bindings |
| 99 | + foreach ($routingKeys as $key) { |
| 100 | + $channel->queue_bind($queue, $exchange_name, $key); |
| 101 | + echo "Binding created: $queue ← $exchange_name ($key)\n"; |
| 102 | + } |
| 103 | + |
| 104 | + $channel->close(); |
| 105 | + $connection->close(); |
| 106 | + echo "Done.\n"; |
| 107 | + |
| 108 | + } catch (Exception $ex) { |
| 109 | + echo "Error: " . $ex->getMessage() . "\n"; |
| 110 | + Log::error($ex); |
| 111 | + exit(1); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments