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

QueueTask::notificationAction()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 74
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 41
nc 2
nop 0
dl 0
loc 74
ccs 0
cts 47
cp 0
crap 30
rs 8.9528
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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