|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gewaer\Notifications\Mobile; |
|
4
|
|
|
|
|
5
|
|
|
use Namshi\Notificator\Notification; |
|
6
|
|
|
use Gewaer\Contracts\PushNotifications as PushNotificationsContract; |
|
7
|
|
|
use Gewaer\Models\Users; |
|
|
|
|
|
|
8
|
|
|
use Gewaer\Models\Notifications; |
|
9
|
|
|
use Phalcon\Di; |
|
10
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
11
|
|
|
|
|
12
|
|
|
class Mobile extends Notification implements PushNotificationsContract |
|
13
|
|
|
{ |
|
14
|
|
|
public $user; |
|
15
|
|
|
|
|
16
|
|
|
public $content; |
|
17
|
|
|
|
|
18
|
|
|
public $systemModule; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(array $user, string $content, string $systemModule) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->user = $user; |
|
23
|
|
|
$this->content = $content; |
|
24
|
|
|
$this->systemModule = $systemModule; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Assemble Notification |
|
29
|
|
|
*/ |
|
30
|
|
|
public function assemble() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->content; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new Apps Notification |
|
37
|
|
|
* @param string $content |
|
38
|
|
|
* @param string $systemModule |
|
39
|
|
|
* @param Users $user |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function apps(string $content, string $systemModule, array $user = null): void |
|
43
|
|
|
{ |
|
44
|
|
|
if (!isset($user)) { |
|
45
|
|
|
$user = Di::getDefault()->getUserData(); |
|
46
|
|
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* Create an array of Apps Push Notification |
|
49
|
|
|
*/ |
|
50
|
|
|
$notificationArray = array( |
|
51
|
|
|
'user'=> $user->toArray(), |
|
52
|
|
|
'content'=> $content, |
|
53
|
|
|
'system_module'=>$systemModule, |
|
54
|
|
|
'notification_type_id'=> Notifications::APPS |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Convert notification to Rabbitmq message |
|
60
|
|
|
*/ |
|
61
|
|
|
$msg = new AMQPMessage(json_encode($notificationArray, JSON_UNESCAPED_SLASHES), ['delivery_mode' => 2]); |
|
62
|
|
|
|
|
63
|
|
|
$channel = Di::getDefault()->getQueue()->channel(); |
|
64
|
|
|
|
|
65
|
|
|
$channel->basic_publish($msg, '', 'notifications'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create a new Users Notification |
|
70
|
|
|
* @param string $content |
|
71
|
|
|
* @param string $systemModule |
|
72
|
|
|
* @param Users $user |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function users(string $content, string $systemModule, Users $user = null): void |
|
76
|
|
|
{ |
|
77
|
|
|
if (!isset($user)) { |
|
78
|
|
|
$user = Di::getDefault()->getUserData(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Create an array of Apps Push Notification |
|
83
|
|
|
*/ |
|
84
|
|
|
$notificationArray = array( |
|
85
|
|
|
'user'=> $user->toArray(), |
|
86
|
|
|
'content'=> $content, |
|
87
|
|
|
'system_module'=>$systemModule, |
|
88
|
|
|
'notification_type_id'=> Notifications::USERS |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Convert notification to Rabbitmq message |
|
94
|
|
|
*/ |
|
95
|
|
|
$msg = new AMQPMessage(json_encode($notificationArray, JSON_UNESCAPED_SLASHES), ['delivery_mode' => 2]); |
|
96
|
|
|
|
|
97
|
|
|
$channel = Di::getDefault()->getQueue()->channel(); |
|
98
|
|
|
|
|
99
|
|
|
$channel->basic_publish($msg, '', 'notifications'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Create a new System Notification |
|
104
|
|
|
* @param string $content |
|
105
|
|
|
* @param string $systemModule |
|
106
|
|
|
* @param Users $user |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function system(string $content, string $systemModule, Users $user = null): void |
|
110
|
|
|
{ |
|
111
|
|
|
if (!isset($user)) { |
|
112
|
|
|
$user = Di::getDefault()->getUserData(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Create an array of Apps Push Notification |
|
117
|
|
|
*/ |
|
118
|
|
|
$notificationArray = array( |
|
119
|
|
|
'user'=> $user->toArray(), |
|
120
|
|
|
'content'=> $content, |
|
121
|
|
|
'system_module'=>$systemModule, |
|
122
|
|
|
'notification_type_id'=> Notifications::SYSTEM |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Convert notification to Rabbitmq message |
|
128
|
|
|
*/ |
|
129
|
|
|
$msg = new AMQPMessage(json_encode($notificationArray, JSON_UNESCAPED_SLASHES), ['delivery_mode' => 2]); |
|
130
|
|
|
|
|
131
|
|
|
$channel = Di::getDefault()->getQueue()->channel(); |
|
132
|
|
|
|
|
133
|
|
|
$channel->basic_publish($msg, '', 'notifications'); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: