Failed Conditions
Pull Request — master (#10)
by Maximo
02:59
created

cli/tasks/PushNotificationTask.php (5 issues)

Labels
Severity
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 Phalcon\Queue\Beanstalk\Extended as BeanstalkExtended;
9
use Phalcon\Queue\Beanstalk\Job;
10
use Sly\NotificationPusher\Adapter\Gcm as GcmAdapter;
11
use Sly\NotificationPusher\Collection\DeviceCollection;
12
use Sly\NotificationPusher\Model\Device;
13
use Sly\NotificationPusher\Model\Message;
14
use Sly\NotificationPusher\Model\Push;
15
use Sly\NotificationPusher\PushManager;
16
use Throwable;
17
18
/**
19
 * CLI To send push ontification and pusher msg
20
 *
21
 * @package Gewaer\Cli\Tasks
22
 *
23
 * @property Config $config
24
 * @property \Pusher\Pusher $config
25
 * @property \Monolog\Logger $log
26
 */
27
class PushNotificationTask extends PhTask
28
{
29
    protected $pushGeneralNotification = 'canvas_notification';
30
31
    /**
32
     * Run the email queue from phalcon CLI
33
     * php cli/app.php Email generalQueue
34
     *
35
     * @return void
36
     */
37
    public function mainAction()
38
    {
39
        //call queue
40
        $queue = new BeanstalkExtended([
41
            'host' => $this->config->beanstalk->host,
0 ignored issues
show
The property beanstalk does not seem to exist on Pusher\Pusher.
Loading history...
42
            'prefix' => $this->config->beanstalk->prefix,
43
        ]);
44
45
        //have 2 different type of queues for now
46
        $queueName = $this->config->pusher->queue;
0 ignored issues
show
The property pusher does not seem to exist on Pusher\Pusher.
Loading history...
47
48
        //dependent variables
49
        $config = $this->config;
50
51
        //call que que tube
52
        $queue->addWorker($queueName, function (Job $job) use ($config) {
53
            try {
54
                //get the array from the queue
55
                $notificationInfo = $job->getBody();
56
57
                $link = !array_key_exists('link', $notificationInfo) ? $this->config->application->siteUrl . '/message/' . $notificationInfo['id'] : $notificationInfo['link'];
0 ignored issues
show
The property application does not seem to exist on Pusher\Pusher.
Loading history...
58
                $linkHtml = " <a href='{$link}'>Link</a>";
59
60
                if ($this->pusher->trigger($notificationInfo['key'], $this->pushGeneralNotification, ['message' => $notificationInfo['message'] . $linkHtml])) {
61
                    $this->log->info("Pusher new {$notificationInfo['message']} to {$notificationInfo['key']}");
62
                } else {
63
                    $this->log->error("Pusher failed {$notificationInfo['message']} to {$notificationInfo['key']}");
64
                }
65
66
                //if we are not sending it to all the Users
67
                if ($notificationInfo['key'] != 'gewaer_general') {
68
                    //find the user Informatio base on its id and try to send the push notification
69
                    $userData = Users::findFirst(str_replace('user_notifications_', '', $notificationInfo['key']));
70
                    $sourceId = '8'; //for now only android
71
72
                    if ($userDevice = UserLinkedSources::findFirst(['conditions' => 'user_id = ?0 and source_id =?1', 'bind' => [$userData->getId(), $sourceId]])) {
73
                        $this->log->addInfo('Pusher Sending push notification');
74
75
                        //send push notification
76
                        $pushManager = new PushManager($config->app->production ? PushManager::ENVIRONMENT_PROD : PushManager::ENVIRONMENT_DEV);
0 ignored issues
show
The property app does not seem to exist on Pusher\Pusher.
Loading history...
77
                        $gcmAdapter = new GcmAdapter([
78
                            'apiKey' => $config->pushNotifcation->android,
0 ignored issues
show
The property pushNotifcation does not seem to exist on Pusher\Pusher.
Loading history...
79
                        ]);
80
81
                        // Set the device(s) to push the notification to.
82
                        $devices = new DeviceCollection([
83
                            new Device($userDevice->source_users_id_text),
84
                        ]);
85
86
                        if (is_null($notificationInfo['id'])) {
87
                            $notificationInfo['id'] = 0;
88
                        }
89
90
                        // Then, create the push skel.
91
                        //$message = new Message($notificationInfo['message']);
92
                        $message = new Message(
93
                            $notificationInfo['message'],
94
                            [
95
                                'id' => time() + $userData->getId(),
96
                                'type' => 'BigText',
97
                                'title' => $notificationInfo['message'],
98
                                'message_id' => $notificationInfo['id']
99
                            ]
100
                        );
101
102
                        // Finally, create and add the push to the manager, and push it!
103
                        $push = new Push($gcmAdapter, $devices, $message);
104
                        $pushManager->add($push);
105
106
                        if ($pushManager->push()) { // Returns a collection of notified devices
107
                            $this->log->addInfo('Pusher Notifaction to Device sent', array_merge($userDevice->toArray(), $notificationInfo));
108
                        } else {
109
                            $this->log->addError('Pusher Fail to push notification to device', array_merge($userDevice->toArray(), $notificationInfo));
110
                        }
111
                    }
112
                }
113
            } catch (Throwable $e) {
114
                $this->log->error($e->getMessage());
115
            }
116
117
            // It's very important to send the right exit code!
118
            exit(0);
119
        });
120
121
        // Start processing queues
122
        $queue->doWork();
123
    }
124
}
125