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

QueueTask::notificationAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 86
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 32
nc 2
nop 0
dl 0
loc 86
ccs 0
cts 38
cp 0
crap 6
rs 9.408
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
10
/**
11
 * CLI To send push ontification and pusher msg
12
 *
13
 * @package Gewaer\Cli\Tasks
14
 *
15
 * @property Config $config
16
 * @property \Pusher\Pusher $pusher
17
 * @property \Monolog\Logger $log
18
 * @property Channel $channel
19
 * @property Queue $queue
20
 * 
21
 */
22
class QueueTask extends PhTask
23
{
24
    public function notificationAction()
25
    {
26
27
         /**
28
     * Every handler needs the below code
29
     */
30
31
        /**
32
         * The job itself
33
         */
34
    //     $jobArray = [
35
    //     'id' => $job_id++,
36
    //     'notification' => 'hello you need to pay your account',
37
    //     'sleep_period' => rand(0, 3)
38
    // ];
39
40
        /**
41
         * Need to convert it to rabbitmq msg
42
         */
43
        $msg = new \PhpAmqpLib\Message\AMQPMessage(
0 ignored issues
show
Unused Code introduced by
The assignment to $msg is dead and can be removed.
Loading history...
44
            json_encode($jobArray, JSON_UNESCAPED_SLASHES),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $jobArray seems to be never defined.
Loading history...
45
            ['delivery_mode' => 2] // make message persistent
46
    );
47
48
        /**
49
         * Actual way to send jobs to queue
50
         */
51
        // $channel->basic_publish($msg, '', RABBITMQ_QUEUE_NAME);
52
53
54
        ///////////////////////////////////////////////////////////////////////
55
56
    
57
        $channel = $this->queue->channel();
58
59
        // Create the queue if it doesnt already exist.
60
        $channel->queue_declare(
61
            $queue = "notifications",
62
            $passive = false,
63
            $durable = true,
64
            $exclusive = false,
65
            $auto_delete = false,
66
            $nowait = false,
67
            $arguments = null,
68
            $ticket = null
69
        );
70
71
        echo ' [*] Waiting for notifications. To exit press CTRL+C', "\n";
72
73
        $callback = function ($msg) {
74
75
            /**
76
             * Assign  message body as an assoc array to job
77
             */
78
            $job = json_decode($msg->body, $assocForm = true);
79
80
            /**
81
             * Custom actions here on jobs
82
             */
83
            echo($job['notification']);
84
            sleep($job['sleep_period']);
85
86
            /**
87
             * Log the delivery info
88
             */
89
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
90
        };
91
92
        $channel->basic_qos(null, 1, null);
93
94
        $channel->basic_consume(
95
            $queue = "notifications",
96
            $consumer_tag = '',
97
            $no_local = false,
98
            $no_ack = false,
99
            $exclusive = false,
100
            $nowait = false,
101
            $callback
102
        );
103
104
        while (count($channel->callbacks)) {
105
            $channel->wait();
106
        }
107
108
        $channel->close();
109
        $this->queue->close();
110
    }
111
}
112