Test Failed
Push — master ( baab8c...6507f4 )
by Maximo
01:50
created

cli/tasks/PushNotificationTask.php (11 issues)

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

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
115
        });
116
117
        // Start processing queues
118
        $queue->doWork();
119
    }
120
}
121