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

PushNotifications   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 120
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A system() 0 25 2
A __construct() 0 3 1
A users() 0 25 2
A apps() 0 24 2
A assemble() 0 3 1
1
<?php
2
3
namespace Gewaer\Notifications\PushNotifications;
4
5
use Namshi\Notificator\Notification;
6
use Gewaer\Contracts\PushNotificationsContract;
7
use Gewaer\Models\Users;
8
use Gewaer\Models\Notifications;
9
use Phalcon\Di;
10
use PhpAmqpLib\Message\AMQPMessage;
11
12
class PushNotifications extends Notification implements PushNotificationsContract
13
{
14
    public $user;
15
16
    public $content;
17
18
    public $systemModule;
19
20
    public function __construct(string $content)
21
    {
22
        $this->content  = $content;
23
    }
24
25
    /**
26
     * Assemble Notification
27
     */
28
    public function assemble()
29
    {
30
        return $this->content;
31
    }
32
33
    /**
34
     * Create a new Apps Notification
35
     * @param string $content
36
     * @param string $systemModule
37
     * @param Users $user
38
     * @return void
39
     */
40
    public static function apps(string $content, string $systemModule, array $user = null): void
41
    {
42
        if (!isset($user)) {
43
            $user =  Di::getDefault()->getUserData();
44
        }
45
        /**
46
         * Create an array of  Apps Push Notification
47
         */
48
        $notificationArray =  array(
49
            'user'=> $user->toArray(),
50
            'content'=> $content,
51
            'system_module'=>$systemModule,
52
            'notification_type_id'=> Notifications::APPS
53
        );
54
55
56
        /**
57
         * Convert notification to Rabbitmq message
58
         */
59
        $msg =  new AMQPMessage(json_encode($notificationArray, JSON_UNESCAPED_SLASHES), ['delivery_mode' => 2]);
60
61
        $channel = Di::getDefault()->getQueue()->channel();
62
63
        $channel->basic_publish($msg, '', 'notifications');
64
    }
65
66
    /**
67
     * Create a new Users Notification
68
     * @param string $content
69
     * @param string $systemModule
70
     * @param Users $user
71
     * @return void
72
     */
73
    public static function users(string $content, string $systemModule, Users $user = null): void
74
    {
75
        if (!isset($user)) {
76
            $user =  Di::getDefault()->getUserData();
77
        }
78
79
        /**
80
         * Create an array of  Apps Push Notification
81
         */
82
        $notificationArray =  array(
83
            'user'=> $user->toArray(),
84
            'content'=> $content,
85
            'system_module'=>$systemModule,
86
            'notification_type_id'=> Notifications::USERS
87
        );
88
89
90
        /**
91
         * Convert notification to Rabbitmq message
92
         */
93
        $msg =  new AMQPMessage(json_encode($notificationArray, JSON_UNESCAPED_SLASHES), ['delivery_mode' => 2]);
94
95
        $channel = Di::getDefault()->getQueue()->channel();
96
97
        $channel->basic_publish($msg, '', 'notifications');
98
    }
99
100
    /**
101
     * Create a new System Notification
102
     * @param string $content
103
     * @param string $systemModule
104
     * @param Users $user
105
     * @return void
106
     */
107
    public static function system(string $content, string $systemModule, Users $user = null): void
108
    {
109
        if (!isset($user)) {
110
            $user =  Di::getDefault()->getUserData();
111
        }
112
113
        /**
114
         * Create an array of  Apps Push Notification
115
         */
116
        $notificationArray =  array(
117
            'user'=> $user->toArray(),
118
            'content'=> $content,
119
            'system_module'=>$systemModule,
120
            'notification_type_id'=> Notifications::SYSTEM
121
        );
122
123
124
        /**
125
         * Convert notification to Rabbitmq message
126
         */
127
        $msg =  new AMQPMessage(json_encode($notificationArray, JSON_UNESCAPED_SLASHES), ['delivery_mode' => 2]);
128
129
        $channel = Di::getDefault()->getQueue()->channel();
130
131
        $channel->basic_publish($msg, '', 'notifications');
132
    }
133
}
134