Test Failed
Branch add-core-tests (42298d)
by Rafael
10:47
created

QueueTask::emailNotificationsAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 61
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 29
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 61
ccs 0
cts 34
cp 0
crap 6
rs 9.456

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 Canvas\Cli\Tasks;
4
5
use Phalcon\Cli\Task as PhTask;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Task was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Canvas\Models\Users;
7
use Canvas\Queue\Queue;
8
use RuntimeException;
9
use Canvas\Notifications\Notification;
10
use Phalcon\Mvc\Model;
11
12
/**
13
 * CLI To send push ontification and pusher msg.
14
 *
15
 * @package Canvas\Cli\Tasks
16
 *
17
 * @property Config $config
18
 * @property \Pusher\Pusher $pusher
19
 * @property \Monolog\Logger $log
20
 * @property Channel $channel
21
 * @property Queue $queue
22
 *
23
 */
24
class QueueTask extends PhTask
25
{
26
    /**
27
     * Queue action for mobile notifications.
28
     * @return void
29
     */
30
    public function mainAction(array $params): void
31
    {
32
        echo "Canvas Ecosystem Queue Jobs: events | notifications".PHP_EOL;
33
    }
34
35
    /**
36
     * Queue to process internal Canvas Events.
37
     *
38
     * @return void
39
     */
40
    public function eventsAction()
41
    {
42
        $callback = function ($msg) {
43
            //we get the data from our event trigger and unserialize
44
            $event = unserialize($msg->body);
45
46
            //overwrite the user who is running this process
47
            if (isset($event['userData']) && $event['userData'] instanceof Users) {
48
                $this->di->setShared('userData', $event['userData']);
49
            }
50
51
            //lets fire the event
52
            $this->events->fire($event['event'], $event['source'], $event['data']);
53
            
54
            echo "Event Fired ({$event['event']})  - Process ID " . $msg->delivery_info['consumer_tag'].PHP_EOL;
55
            $this->log->info("Notification ({$event['event']}) - Process ID " . $msg->delivery_info['consumer_tag']);
56
        };
57
58
        Queue::process(QUEUE::EVENTS, $callback);
59
    }
60
61
    /**
62
     * Queue to process internal Canvas Events.
63
     *
64
     * @return void
65
     */
66
    public function notificationsAction()
67
    {
68
        $callback = function ($msg) {
69
            //we get the data from our event trigger and unserialize
70
            $notification = unserialize($msg->body);
71
            
72
            //overwrite the user who is running this process
73
            if ($notification['from'] instanceof Users) {
74
                $this->di->setShared('userData', $notification['from']);
75
            }
76
77
            if (!$notification['to'] instanceof Users) {
78
                echo 'Attribute TO has to be a User'.PHP_EOL;
79
                return;
80
            }
81
82
            if (!class_exists($notification['notification'])) {
83
                echo 'Attribute notification has to be a Notificatoin'.PHP_EOL;
84
                return;
85
            }
86
            $notificationClass = $notification['notification'];
87
88
            if (!$notification['entity'] instanceof Model) {
89
                echo 'Attribute entity has to be a Model'.PHP_EOL;
90
                return;
91
            }
92
93
            $user = $notification['to'];
94
95
            //instance notification and pass the entity
96
            $notification = new $notification['notification']($notification['entity']);
97
            //disable the queue so we process it now
98
            $notification->disableQueue();
99
100
            //run notify for the specifiy user
101
            $user->notify($notification);
102
            
103
            echo "Notification ({$notificationClass}) sent to {$user->email} - Process ID " . $msg->delivery_info['consumer_tag'].PHP_EOL;
104
            $this->log->info("Notification ({$notificationClass}) sent to {$user->email} - Process ID " . $msg->delivery_info['consumer_tag']);
105
        };
106
107
        Queue::process(QUEUE::NOTIFICATIONS, $callback);
108
    }
109
}
110