Completed
Push — master ( 5a7fc1...edb844 )
by
06:05
created

NotificationListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 109
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A push() 0 22 3
B handle() 0 28 2
A subscribe() 0 6 2
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
     * 订阅的时间和处理的方法.
16
     *
17
     * @var array
18
     */
19
    protected $subscribe_events = [
20
        TopicUpVoted::class => 'handle',
21
        NewReply::class     => 'handle',
22
    ];
23
24
    /**
25
     * Jpush 对象
26
     *
27
     * @var Jpush
28
     */
29
    private $jpush = null;
30
    /**
31
     * @var NotificationRepositoryInterface
32
     */
33
    private $notifications;
34
35
    /**
36
     * PushNotificationHandler constructor.
37
     *
38
     * @param NotificationRepositoryInterface $notifications
39
     */
40
    public function __construct(NotificationRepositoryInterface $notifications)
41
    {
42
        $this->notifications = $notifications;
43
    }
44
45
    /**
46
     * 推送消息.
47
     *
48
     * @param $user_ids
49
     * @param $msg
50
     * @param $extras
51
     */
52
    protected function push($user_ids, $msg, $extras = null)
53
    {
54
        if (! $this->jpush) {
55
            $this->jpush = new Jpush();
56
        }
57
58
        $user_ids = (array) $user_ids;
59
        $user_ids = array_map(function ($user_id) {
60
            return 'userid_'.$user_id;
61
        }, $user_ids);
62
63
        try {
64
            $this->jpush
65
                ->platform('all')
66
                ->message($msg)
67
                ->toAlias($user_ids)
68
                ->extras($extras)
69
                ->send();
70
        } catch (APIRequestException $e) {
71
            // Ignore
72
        }
73
    }
74
75
    /**
76
     * Handle the event.
77
     *
78
     * @param Event|TopicUpVoted $event
79
     */
80
    public function handle(Event $event)
81
    {
82
        $data = [
83
            '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...
84
            '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...
85
            '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...
86
            '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...
87
            '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...
88
            'reply_id'     => $event->getReplyId(),
89
        ];
90
91
        $notification = $this->notifications->store($data);
92
93
        $presenter = app('autopresenter')->decorate($notification);
94
95
        $push_data = array_only($data, [
96
            'topic_id',
97
            'from_user_id',
98
            'type',
99
        ]);
100
101
        if ($data['reply_id'] !== 0) {
102
            $push_data['reply_id'] = $data['reply_id'];
103
            $push_data['replies_url'] = route('replies.web_view', $data['topic_id']);
104
        }
105
106
        $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...
107
    }
108
109
    /**
110
     * 注册监听器给订阅者。.
111
     *
112
     * @param \Illuminate\Events\Dispatcher $events
113
     */
114
    public function subscribe($events)
115
    {
116
        foreach ($this->subscribe_events as $event => $handle_method) {
117
            $events->listen($event, self::class.'@'.$handle_method);
118
        }
119
    }
120
}
121