Test Failed
Pull Request — master (#70)
by Rafael
05:48
created

QueueTask   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 76
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B notificationAction() 0 74 5
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\Mobile\Apps as AppsPushNotifications;
11
use Gewaer\Notifications\Mobile\Users as UsersPushNotifications;
12
use Gewaer\Notifications\Mobile\System as 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
                 default:
69
                     break;
70
             }
71
72
73
            /**
74
             * Trigger Event Manager
75
             */
76
            Di::getDefault()->getManager()->trigger($notification);
77
78
            /**
79
             * Log the delivery info
80
             */
81
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
82
        };
83
84
        $channel->basic_qos(null, 1, null);
85
86
        $channel->basic_consume(
87
            $queue = "notifications",
88
            $consumer_tag = '',
89
            $no_local = false,
90
            $no_ack = false,
91
            $exclusive = false,
92
            $nowait = false,
93
            $callback
94
        );
95
96
        while (count($channel->callbacks)) {
97
            $channel->wait();
98
        }
99
100
        $channel->close();
101
        $this->queue->close();
102
    }
103
}
104