Gateway::sendPush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 2
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * @namespace
4
 */
5
namespace Application\Push;
6
7
use Application\Exception;
0 ignored issues
show
Bug introduced by
The type Application\Exception 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...
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) {
0 ignored issues
show
introduced by
$push is of type Application\Push\Row, thus it always evaluated to true.
Loading history...
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