bakaphp /
phalcon-api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Gewaer\Cli\Tasks; |
||
| 4 | |||
| 5 | use Phalcon\Cli\Task as PhTask; |
||
| 6 | use Gewaer\Models\UserLinkedSources; |
||
| 7 | use Gewaer\Models\Users; |
||
| 8 | use Throwable; |
||
| 9 | use Phalcon\Di; |
||
| 10 | use Gewaer\Notifications\PushNotifications\AppsPushNotifications; |
||
| 11 | use Gewaer\Notifications\PushNotifications\UsersPushNotifications; |
||
| 12 | use Gewaer\Notifications\PushNotifications\SystemPushNotifications; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * CLI To send push ontification and pusher msg |
||
| 16 | * |
||
| 17 | * @package Gewaer\Cli\Tasks |
||
| 18 | * |
||
| 19 | * @property Config $config |
||
| 20 | * @property \Pusher\Pusher $pusher |
||
| 21 | * @property \Monolog\Logger $log |
||
| 22 | * @property Channel $channel |
||
| 23 | * @property Queue $queue |
||
| 24 | * |
||
| 25 | */ |
||
| 26 | class QueueTask extends PhTask |
||
| 27 | { |
||
| 28 | public function notificationAction() |
||
| 29 | { |
||
| 30 | $channel = $this->queue->channel(); |
||
| 31 | |||
| 32 | // Create the queue if it doesnt already exist. |
||
| 33 | $channel->queue_declare( |
||
| 34 | $queue = "notifications", |
||
| 35 | $passive = false, |
||
| 36 | $durable = true, |
||
| 37 | $exclusive = false, |
||
| 38 | $auto_delete = false, |
||
| 39 | $nowait = false, |
||
| 40 | $arguments = null, |
||
| 41 | $ticket = null |
||
| 42 | ); |
||
| 43 | |||
| 44 | echo ' [*] Waiting for notifications. To exit press CTRL+C', "\n"; |
||
| 45 | |||
| 46 | $callback = function ($msg) { |
||
| 47 | $msgObject = json_decode($msg->body); |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | echo ' [x] Received from system module: ',$msgObject->system_module, "\n"; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Lets determine what type of notification we are dealing with |
||
| 56 | */ |
||
| 57 | switch ($msgObject->notification_type_id) { |
||
| 58 | case 1: |
||
| 59 | $notification = new AppsPushNotifications((array)$msgObject->user, $msgObject->content, $msgObject->system_module); |
||
| 60 | break; |
||
| 61 | case 2: |
||
| 62 | $notification = new UsersPushNotifications((array)$msgObject->user, $msgObject->content, $msgObject->system_module); |
||
| 63 | break; |
||
| 64 | |||
| 65 | case 3: |
||
| 66 | $notification = new SystemPushNotifications((array)$msgObject->user, $msgObject->content, $msgObject->system_module); |
||
| 67 | break; |
||
| 68 | |||
| 69 | default: |
||
| 70 | # code... |
||
| 71 | break; |
||
| 72 | } |
||
|
1 ignored issue
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Trigger Event Manager |
||
| 77 | */ |
||
| 78 | Di::getDefault()->getManager()->trigger($notification); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Log the delivery info |
||
| 82 | */ |
||
| 83 | $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); |
||
| 84 | }; |
||
| 85 | |||
| 86 | $channel->basic_qos(null, 1, null); |
||
| 87 | |||
| 88 | $channel->basic_consume( |
||
| 89 | $queue = "notifications", |
||
| 90 | $consumer_tag = '', |
||
| 91 | $no_local = false, |
||
| 92 | $no_ack = false, |
||
| 93 | $exclusive = false, |
||
| 94 | $nowait = false, |
||
| 95 | $callback |
||
| 96 | ); |
||
| 97 | |||
| 98 | while (count($channel->callbacks)) { |
||
| 99 | $channel->wait(); |
||
| 100 | } |
||
| 101 | |||
| 102 | $channel->close(); |
||
| 103 | $this->queue->close(); |
||
| 104 | } |
||
| 105 | } |
||
| 106 |