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

QueueTask::notificationAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 86
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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