NotificationListener::subscribe()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PHPHub\Listeners;
4
5
use JPush\Exception\APIRequestException;
6
use PHPHub\Events\Event;
7
use PHPHub\Events\NewReply;
8
use PHPHub\Events\TopicUpVoted;
9
use PHPHub\Repositories\NotificationRepositoryInterface;
10
use PHPHub\Services\PushService\Jpush;
11
12
class NotificationListener
13
{
14
    /**
15
     * Jpush 对象
16
     *
17
     * @var Jpush
18
     */
19
    private $jpush = null;
20
21
    /**
22
     * @var NotificationRepositoryInterface
23
     */
24
    private $notifications;
25
26
    /**
27
     * PushNotificationHandler constructor.
28
     *
29
     * @param NotificationRepositoryInterface $notifications
30
     */
31
    public function __construct(NotificationRepositoryInterface $notifications)
32
    {
33
        $this->notifications = $notifications;
34
    }
35
36
    /**
37
     * 推送消息.
38
     *
39
     * @param $user_ids
40
     * @param $msg
41
     * @param $extras
42
     */
43
    protected function push($user_ids, $msg, $extras = null)
44
    {
45
        if (! $this->jpush) {
46
            $this->jpush = new Jpush();
47
        }
48
49
        $user_ids = (array) $user_ids;
50
        $user_ids = array_map(function ($user_id) {
51
            return 'userid_'.$user_id;
52
        }, $user_ids);
53
54
        try {
55
            $this->jpush
56
                ->platform('all')
57
                ->message($msg)
58
                ->toAlias($user_ids)
59
                ->extras($extras)
60
                ->send();
61
        } catch (APIRequestException $e) {
62
            // Ignore
63
        }
64
    }
65
66
    /**
67
     * Handle the event.
68
     *
69
     * @param Event|TopicUpVoted $event
70
     */
71
    public function handle(Event $event)
72
    {
73
        $data = [
74
            'topic_id'     => $event->getTopicId(),
0 ignored issues
show
Bug introduced by
The method getTopicId() does not seem to exist on object<PHPHub\Events\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            'body'         => $event->getBody(),
0 ignored issues
show
Bug introduced by
The method getBody() does not seem to exist on object<PHPHub\Events\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            'from_user_id' => $event->getFromUserId(),
0 ignored issues
show
Bug introduced by
The method getFromUserId() does not seem to exist on object<PHPHub\Events\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            'user_id'      => $event->getUserId(),
0 ignored issues
show
Bug introduced by
The method getUserId() does not seem to exist on object<PHPHub\Events\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            'type'         => $event->getType(),
0 ignored issues
show
Bug introduced by
The method getType() does not seem to exist on object<PHPHub\Events\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
            'reply_id'     => $event->getReplyId(),
80
        ];
81
82
        $notification = $this->notifications->store($data);
83
84
        $presenter = app('autopresenter')->decorate($notification);
85
86
        $push_data = array_only($data, [
87
            'topic_id',
88
            'from_user_id',
89
            'type',
90
        ]);
91
92
        if ($data['reply_id'] !== 0) {
93
            $push_data['reply_id'] = $data['reply_id'];
94
            $push_data['replies_url'] = route('replies.web_view', $data['topic_id']);
95
        }
96
97
        $this->push($event->getUserId(), $presenter->message(), $push_data);
0 ignored issues
show
Bug introduced by
The method getUserId() does not seem to exist on object<PHPHub\Events\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
    }
99
100
    /**
101
     * 注册监听器给订阅者。.
102
     *
103
     * @param \Illuminate\Events\Dispatcher $events
104
     */
105
    public function subscribe($events)
106
    {
107
        $events->listen(TopicUpVoted::class, class_callback(self::class, 'handle'));
108
        $events->listen(NewReply::class, class_callback(self::class, 'handle'));
109
    }
110
}
111