NotificationsTable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 86
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 12 1
B findMap() 0 54 5
1
<?php
2
namespace App\Model\Table;
3
4
use Cake\ORM\Query;
5
use Cake\ORM\Table;
6
use Cake\Routing\Router;
7
use Cake\Utility\Text;
8
9
class NotificationsTable extends Table
10
{
11
12
    /**
13
     * Initialize method
14
     *
15
     * @param array $config The configuration for the Table.
16
     *
17
     * @return void
18
     */
19
    public function initialize(array $config)
20
    {
21
        $this->table('notifications');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
        $this->displayField('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24
25
        $this->addBehavior('Timestamp');
26
27
        $this->belongsTo('Users', [
28
            'foreignKey' => 'user_id'
29
        ]);
30
    }
31
32
    /**
33
     * Custom finder for map the ntofications.
34
     *
35
     * @param \Cake\ORM\Query $query The query finder.
36
     * @param array $options The options passed in the query builder.
37
     *
38
     * @return \Cake\ORM\Query
39
     */
40
    public function findMap(Query $query, array $options)
41
    {
42
        return $query
43
            ->formatResults(function ($notifications) use ($options) {
44
                return $notifications->map(function ($notification) use ($options) {
45
                    $notification->data = unserialize($notification->data);
46
47
                    switch ($notification->type) {
48
                        case 'conversation.reply':
49
                            $username = $notification->data['sender']->username;
50
                            $conversationTitle = Text::truncate($notification->data['conversation']->title, 50, ['ellipsis' => '...', 'exact' => false]);
51
52
                            //Check if the creator of the conversation is the current user.
53
                            if ($notification->data['conversation']->user_id === $options['session']->read('Auth.User.id')) {
54
                                $notification->text = __(
55
                                    '<strong>{0}</strong> has replied in your conversation <strong>{1}</strong>.',
56
                                    h($username),
57
                                    h($conversationTitle)
58
                                );
59
                            } else {
60
                                $notification->text = __(
61
                                    '<strong>{0}</strong> has replied in the conversation <strong>{1}</strong>.',
62
                                    h($username),
63
                                    h($conversationTitle)
64
                                );
65
                            }
66
67
                            $notification->link = Router::url(['controller' => 'conversations', 'action' => 'go', $notification->data['conversation']->last_message_id, 'prefix' => false]);
68
                            break;
69
70
                        case 'bot':
71
                            $notification->text = __(
72
                                'Welcome on <strong>{0}</strong>! You can now post your first comment in the blog.',
73
                                \Cake\Core\Configure::read('Site.name')
74
                            );
75
76
                            $notification->link = Router::url(['controller' => 'blog', 'action' => 'index', 'prefix' => false]);
77
                            $notification->icon = $notification->data['icon'];
78
                            break;
79
80
                        case 'badge':
81
                            $notification->text = __(
82
                                'You have unlock the badge "{0}".',
83
                                $notification->data['badge']->name
84
                            );
85
86
                            $notification->link = Router::url(['_name' => 'users-profile', 'id' => $notification->data['user']->id, 'slug' => $notification->data['user']->username, '#' => 'badges', 'prefix' => false]);
87
                            break;
88
                    }
89
90
                    return $notification;
91
                });
92
            });
93
    }
94
}
95