|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @namespace |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Application\Push; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Exception; |
|
|
|
|
|
|
8
|
|
|
use Bluz\Db\Exception\DbException; |
|
9
|
|
|
use Bluz\Db\Exception\InvalidPrimaryKeyException; |
|
10
|
|
|
use Bluz\Proxy\Config; |
|
11
|
|
|
use Bluz\Proxy\Router; |
|
12
|
|
|
use Minishlink\WebPush\WebPush; |
|
13
|
|
|
use Minishlink\WebPush\Subscription; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Gateway |
|
17
|
|
|
* |
|
18
|
|
|
* @package Application\Push |
|
19
|
|
|
* @author Anton Shevchuk |
|
20
|
|
|
*/ |
|
21
|
|
|
class Gateway |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Send push notification |
|
25
|
|
|
* |
|
26
|
|
|
* @param Row $push |
|
27
|
|
|
* @param string $message |
|
28
|
|
|
* |
|
29
|
|
|
* @return array|bool |
|
30
|
|
|
* @throws \ErrorException |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function sendPush(Row $push, $message) |
|
33
|
|
|
{ |
|
34
|
|
|
$subscription = Subscription::create([ |
|
35
|
|
|
'contentEncoding' => $push->contentEncoding, |
|
36
|
|
|
'endpoint' => $push->endpoint, |
|
37
|
|
|
'authToken' => $push->authToken, |
|
38
|
|
|
'publicKey' => $push->publicKey, |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
|
|
$auth = Config::get('module.push'); |
|
42
|
|
|
|
|
43
|
|
|
$webPush = new WebPush($auth); |
|
44
|
|
|
|
|
45
|
|
|
return $webPush->sendNotification( |
|
46
|
|
|
$subscription, |
|
47
|
|
|
json_encode([ |
|
48
|
|
|
'icon' => Router::getBaseUrl() . 'img/icon-512x512.png', |
|
49
|
|
|
'badge' => Router::getBaseUrl() . 'img/icon-128x128.png', |
|
50
|
|
|
'body' => $message |
|
51
|
|
|
]), |
|
52
|
|
|
true |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Send notification by push Id |
|
58
|
|
|
* |
|
59
|
|
|
* @param integer $pushId |
|
60
|
|
|
* @param string $message |
|
61
|
|
|
* |
|
62
|
|
|
* @return array|bool |
|
63
|
|
|
* @throws DbException |
|
64
|
|
|
* @throws Exception |
|
65
|
|
|
* @throws InvalidPrimaryKeyException |
|
66
|
|
|
* @throws \ErrorException |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function sendByPushId($pushId, $message) |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var Row $push */ |
|
71
|
|
|
$push = Table::findRow($pushId); |
|
72
|
|
|
|
|
73
|
|
|
if (!$push) { |
|
|
|
|
|
|
74
|
|
|
throw new Exception('Invalid push ID'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return self::sendPush($push, $message); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Send notifications by user Id |
|
82
|
|
|
* |
|
83
|
|
|
* @param $userId |
|
84
|
|
|
* @param $message |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
* @throws DbException |
|
88
|
|
|
* @throws \ErrorException |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function sendByUserId($userId, $message): void |
|
91
|
|
|
{ |
|
92
|
|
|
$pushes = Table::findWhere(['userId' => $userId]); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($pushes as $push) { |
|
95
|
|
|
self::sendPush($push, $message); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths