Completed
Pull Request — master (#178)
by Fèvre
02:40
created

NotificationsTable::findMap()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.7449
c 0
b 0
f 0
cc 5
eloc 35
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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');
22
        $this->displayField('id');
23
        $this->primaryKey('id');
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 message in the Forum.',
73
                                \Cake\Core\Configure::read('Site.name')
74
                            );
75
76
                            $notification->link = Router::url(['controller' => 'forum', 'action' => 'index', 'prefix' => 'forum']);
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