PushNotifications   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 121
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B handle() 0 55 8
A oneSignal() 0 14 1
1
<?php
2
3
namespace Canvas\Cli\Jobs;
4
5
use Canvas\Contracts\Queue\QueueableJobInterface;
6
use Canvas\Jobs\Job;
7
use Phalcon\Di;
8
use GuzzleHttp\Client as GuzzleClient;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client 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...
9
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
0 ignored issues
show
Bug introduced by
The type Http\Adapter\Guzzle6\Client 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...
10
use Http\Client\Common\HttpMethodsClient as HttpClient;
0 ignored issues
show
Bug introduced by
The type Http\Client\Common\HttpMethodsClient 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 Http\Message\MessageFactory\GuzzleMessageFactory;
0 ignored issues
show
Bug introduced by
The type Http\Message\MessageFactory\GuzzleMessageFactory 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 OneSignal\Config;
0 ignored issues
show
Bug introduced by
The type OneSignal\Config 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 OneSignal\OneSignal;
0 ignored issues
show
Bug introduced by
The type OneSignal\OneSignal 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 Canvas\Models\UserLinkedSources;
15
use Canvas\Notifications\PushNotification;
16
use Exception;
17
18
class PushNotifications extends Job implements QueueableJobInterface
19
{
20
    /**
21
     * Realtime channel.
22
     *
23
     * @var string
24
     */
25
    protected $users;
26
27
    /**
28
     * Realtime event.
29
     *
30
     * @var string
31
     */
32
    protected $message;
33
34
    /**
35
     * Realtime params.
36
     *
37
     * @var array
38
     */
39
    protected $params;
40
41
    /**
42
     * Constructor setup info for Pusher.
43
     *
44
     * @param string $channel
45
     * @param string $event
46
     * @param array $params
47
     */
48
    public function __construct(PushNotification $pushNotification)
49
    {
50
        $this->users = $pushNotification->to;
51
        $this->message = $pushNotification->message;
52
        $this->params = $pushNotification->params;
53
    }
54
55
    /**
56
     * Handle the pusher request.
57
     *
58
     * @return void
59
     */
60
    public function handle()
61
    {
62
        $config = Di::getDefault()->getConfig();
63
64
        if (empty($this->users)) {
65
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
66
        }
67
68
        $userDevicesArray = UserLinkedSources::getMobileUserLinkedSources($this->users->getId());
69
70
        if (empty($userDevicesArray[2]) && empty($userDevicesArray[3])) {
71
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
72
        }
73
74
        /**
75
         * One signal array params.
76
         */
77
        $pushBody = [
78
            'contents' => [
79
                'en' => $this->message
80
            ],
81
            'data' => $this->params
82
        ];
83
84
        /**
85
         * @todo change to use some constante , ID dont tell you what device it is
86
         */
87
        //send push android
88
        if (!empty($userDevicesArray[2])) {
89
            $pushBody['include_player_ids'][] = $userDevicesArray[2][0];
90
        }
91
92
        //ios
93
        if (!empty($userDevicesArray[3])) {
94
            $pushBody['include_player_ids'][] = $userDevicesArray[3][0];
95
        }
96
97
        try {
98
            $this->oneSignal(
99
                $config->pushNotifications->appId,
100
                $config->pushNotifications->authKey,
101
                $config->pushNotifications->userAuthKey
102
            )->notifications->add(
103
                $pushBody
104
            );
105
        } catch (Exception $e) {
106
            if (Di::getDefault()->has('log')) {
107
                Di::getDefault()->get('log')->error(
108
                    'Error sending push notification via OneSignal - ' . $e->getMessage(),
109
                    [$e->getTraceAsString()]
110
                );
111
            }
112
        }
113
114
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
115
    }
116
117
    /**
118
     * Give back a one signal object to send push notifications.
119
     *
120
     * @param string $appId
121
     * @param string $appAuthKey
122
     * @param string $appUserKey
123
     * @return OneSignal
124
     */
125
    private function oneSignal(string $appId, string $appAuthKey, string $appUserKey): OneSignal
126
    {
127
        $config = new Config();
128
        $config->setApplicationId($appId);
129
        $config->setApplicationAuthKey($appAuthKey);
130
        $config->setUserAuthKey($appUserKey);
131
132
        $guzzle = new GuzzleClient([
133
            'timeout' => 2.0,
134
        ]);
135
136
        $client = new HttpClient(new GuzzleAdapter($guzzle), new GuzzleMessageFactory());
137
138
        return  new OneSignal($config, $client);
139
    }
140
}
141