Completed
Push — master ( d93525...41af6a )
by Fèvre
10s
created

src/Event/Notifications.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Event;
3
4
use Cake\Event\Event;
5
use Cake\Event\EventListenerInterface;
6
use Cake\ORM\TableRegistry;
7
8
class Notifications implements EventListenerInterface
9
{
10
11
    /**
12
     * ImplementedEvents method.
13
     *
14
     * @return array
15
     */
16
    public function implementedEvents()
17
    {
18
        return [
19
            'Model.Notifications.new' => 'newNotification',
20
            'Model.Notifications.dispatch' => 'dispatchParticipants'
21
        ];
22
    }
23
24
    /**
25
     * We send a new notification to an user.
26
     *
27
     * @param \Cake\Event\Event $event The event that was fired.
28
     *
29
     * @return false
30
     */
31
    public function newNotification(Event $event)
32
    {
33
        $this->Notifications = TableRegistry::get('Notifications');
34
        $type = $event->getData('type');
35
36
        if (is_null($type)) {
37
            return false;
38
        }
39
        $type = strtolower($type);
40
41
        switch ($type) {
42
            case 'conversation.reply':
43
                $result = $this->_conversationReply($event);
44
                break;
45
46
            case 'bot':
47
                $result = $this->_bot($event);
48
                break;
49
50
            case 'badge':
51
                $result = $this->_badge($event);
52
                break;
53
            default:
54
                $result = false;
55
        }
56
57
        return $result;
58
    }
59
60
    /**
61
     * Dispatch notification for the participants of a conversation.
62
     *
63
     * @param \Cake\Event\Event $event The event that was fired.
64
     *
65
     * @return false
66
     */
67
    public function dispatchParticipants(Event $event)
68
    {
69
        $this->ConversationsUsers = TableRegistry::get('ConversationsUsers');
70
71
        $participants = $this->ConversationsUsers
72
            ->find()
73
            ->where([
74
                'ConversationsUsers.conversation_id' => $event->getData('conversation_id')
75
            ])
76
            ->contain([
77
                'Users' => function ($q) {
78
                    return $q->select([
79
                        'id'
80
                    ]);
81
                }
82
            ])
83
            ->select([
84
                'ConversationsUsers.id',
85
                'ConversationsUsers.conversation_id',
86
                'ConversationsUsers.user_id'
87
            ])
88
            ->toArray();
89
90
        if (empty($participants)) {
91
            return true;
92
        }
93
94
        foreach ($participants as $participant) {
95
            if ($participant->user_id != $event->getData('sender_id')) {
96
                $event->setData('participant', $participant);
97
98
                $this->newNotification($event);
99
            }
100
        }
101
102
        return true;
103
    }
104
105
    /**
106
     * A user has replied to a conversation.
107
     *
108
     * @param \Cake\Event\Event $event The event that was fired.
109
     *
110
     * @return bool
111
     */
112
    protected function _conversationReply(Event $event)
113
    {
114
        if (!is_integer($event->getData('conversation_id'))) {
115
            return false;
116
        }
117
118
        $this->Conversations = TableRegistry::get('Conversations');
0 ignored issues
show
The property Conversations does not seem to exist. Did you mean ConversationsUsers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
119
        $this->Users = TableRegistry::get('Users');
0 ignored issues
show
The property Users does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
120
121
        $conversation = $this->Conversations
0 ignored issues
show
The property Conversations does not seem to exist. Did you mean ConversationsUsers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
122
            ->find()
123
            ->where([
124
                'Conversations.id' => $event->getData('conversation_id')
125
            ])
126
            ->select([
127
                'id', 'user_id', 'title', 'last_message_id'
128
            ])
129
            ->first();
130
131
        $sender = $this->Users
132
            ->find('medium')
133
            ->where([
134
                'Users.id' => $event->getData('sender_id')
135
            ])
136
            ->first();
137
138
        //Check if this user hasn't already a notification. (Prevent for spam)
139
        $hasReplied = $this->Notifications
140
            ->find()
141
            ->where([
142
                'Notifications.foreign_key' => $conversation->id,
143
                'Notifications.type' => $event->getData('type'),
144
                'Notifications.user_id' => $event->getData('participant')->user->id
145
            ])
146
            ->first();
147
148
        if (!is_null($hasReplied)) {
149
            $hasReplied->data = serialize(['sender' => $sender, 'conversation' => $conversation]);
150
            $hasReplied->is_read = 0;
151
152
            $this->Notifications->save($hasReplied);
153
        } else {
154
            $data = [];
155
            $data['user_id'] = $event->getData('participant')->user->id;
156
            $data['type'] = $event->getData('type');
157
            $data['data'] = serialize(['sender' => $sender, 'conversation' => $conversation]);
158
            $data['foreign_key'] = $conversation->id;
159
160
            $entity = $this->Notifications->newEntity($data);
161
            $this->Notifications->save($entity);
162
        }
163
164
        return true;
165
    }
166
167
    /**
168
     * A user has sign up on the website.
169
     *
170
     * @param \Cake\Event\Event $event The event that was fired.
171
     *
172
     * @return bool
173
     */
174
    protected function _bot(Event $event)
175
    {
176
        $this->Users = TableRegistry::get('Users');
177
178
        $user = $this->Users
179
            ->find()
180
            ->where(['id' => $event->getData('user_id')])
181
            ->first();
182
183
        if (is_null($user)) {
184
            return false;
185
        }
186
187
        $data = [];
188
        $data['user_id'] = $user->id;
189
        $data['type'] = $event->getData('type');
190
        $data['data'] = serialize(['icon' => '../img/notifications/welcome.png']);
191
192
        $entity = $this->Notifications->newEntity($data);
193
        $this->Notifications->save($entity);
194
195
        return true;
196
    }
197
198
    /**
199
     * A user has unlock a badge.
200
     *
201
     * @param \Cake\Event\Event $event The event that was fired.
202
     *
203
     * @return bool
204
     */
205
    protected function _badge(Event $event)
206
    {
207
        $this->Badges = TableRegistry::get('Badges');
208
        $this->Notifications = TableRegistry::get('Notifications');
209
210
        $badge = $this->Badges
211
            ->find()
212
            ->where(['Badges.id' => $event->getData('badge')->badge_id])
213
            ->first();
214
215
        if (is_null($badge)) {
216
            return false;
217
        }
218
219
        $this->Users = TableRegistry::get('Users');
220
221
        $user = $this->Users
222
            ->find()
223
            ->where(['id' => $event->getData('badge')->user_id])
224
            ->select([
225
                'id'
226
            ])
227
            ->first();
228
229
        if (is_null($user)) {
230
            return false;
231
        }
232
233
        $data = [];
234
        $data['user_id'] = $event->getData('badge')->user_id;
235
        $data['type'] = $event->getData('type');
236
        $data['data'] = serialize(['badge' => $badge, 'user' => $user]);
237
238
        $entity = $this->Notifications->newEntity($data);
239
        $this->Notifications->save($entity);
240
241
        return true;
242
    }
243
}
244