Completed
Push — master ( d44ef5...c7e879 )
by Fèvre
12s
created

LogSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 147
Duplicated Lines 41.5 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 7
dl 61
loc 147
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A subscribe() 0 6 2
A categoryWasChanged() 14 14 1
A conversationWasLocked() 10 10 1
A conversationWasPinned() 10 10 1
A postWasDeleted() 13 13 1
A titleWasChanged() 14 14 1
A create() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Xetaravel\Listeners\Subscribers\Discuss;
3
4
use Carbon\Carbon;
5
use Xetaravel\Events\Discuss\CategoryWasChangedEvent;
6
use Xetaravel\Events\Discuss\ConversationWasLockedEvent;
7
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent;
8
use Xetaravel\Events\Discuss\PostWasDeletedEvent;
9
use Xetaravel\Events\Discuss\TitleWasChangedEvent;
10
use Xetaravel\Models\DiscussLog;
11
use Xetaravel\Models\User;
12
use Xetaravel\Notifications\BadgeNotification;
13
14
class LogSubscriber
15
{
16
    /**
17
     * The events mapping to the listener function.
18
     *
19
     * @var array
20
     */
21
    protected $events = [
22
        CategoryWasChangedEvent::class => 'categoryWasChanged',
23
        ConversationWasLockedEvent::class => 'conversationWasLocked',
24
        ConversationWasPinnedEvent::class => 'conversationWasPinned',
25
        PostWasDeletedEvent::class => 'postWasDeleted',
26
        TitleWasChangedEvent::class => 'titleWasChanged'
27
    ];
28
29
    /**
30
     * Register the listeners for the subscriber.
31
     *
32
     * @param Illuminate\Events\Dispatcher $events
33
     *
34
     * @return void
35
     */
36
    public function subscribe($events)
37
    {
38
        foreach ($this->events as $event => $action) {
39
            $events->listen($event, LogSubscriber::class . '@' . $action);
40
        }
41
    }
42
43
    /**
44
     * Handle a CategoryWasChanged event.
45
     *
46
     * @param \Xetaravel\Events\Discuss\CategoryWasChangedEvent $event The event that was fired.
47
     *
48
     * @return bool
49
     */
50 View Code Duplication
    public function categoryWasChanged(CategoryWasChangedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $data = [
53
            'loggable_id' => $event->conversation->getKey(),
54
            'loggable_type' => get_class($event->conversation),
55
            'event_type' => CategoryWasChangedEvent::class,
56
            'data' => [
57
                'old' => $event->oldCategory,
58
                'new' => $event->category
59
            ]
60
        ];
61
62
        return $this->create($data);
63
    }
64
65
    /**
66
     * Handle a ConversationWasLocked event.
67
     *
68
     * @param \Xetaravel\Events\Discuss\ConversationWasLockedEvent $event The event that was fired.
69
     *
70
     * @return bool
71
     */
72 View Code Duplication
    public function conversationWasLocked(ConversationWasLockedEvent $event): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $data = [
75
            'loggable_id' => $event->conversation->getKey(),
76
            'loggable_type' => get_class($event->conversation),
77
            'event_type' => ConversationWasLockedEvent::class
78
        ];
79
80
        return $this->create($data);
81
    }
82
83
    /**
84
     * Handle a ConversationWasPinned event.
85
     *
86
     * @param \Xetaravel\Events\Discuss\ConversationWasPinnedEvent $event The event that was fired.
87
     *
88
     * @return bool
89
     */
90 View Code Duplication
    public function conversationWasPinned(ConversationWasPinnedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $data = [
93
            'loggable_id' => $event->conversation->getKey(),
94
            'loggable_type' => get_class($event->conversation),
95
            'event_type' => ConversationWasPinnedEvent::class
96
        ];
97
98
        return $this->create($data);
99
    }
100
101
    /**
102
     * Handle a PostWasDeleted event.
103
     *
104
     * @param \Xetaravel\Events\Discuss\PostWasDeletedEvent $event The event that was fired.
105
     *
106
     * @return bool
107
     */
108 View Code Duplication
    public function postWasDeleted(PostWasDeletedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $data = [
111
            'loggable_id' => $event->conversation->getKey(),
112
            'loggable_type' => get_class($event->conversation),
113
            'event_type' => PostWasDeletedEvent::class,
114
            'data' => [
115
                'post_user_id' => $event->user->getKey()
116
            ]
117
        ];
118
119
        return $this->create($data);
120
    }
121
122
    /**
123
     * Handle a TitleWasChanged event.
124
     *
125
     * @param \Xetaravel\Events\Discuss\TitleWasChangedEvent $event The event that was fired.
126
     *
127
     * @return bool
128
     */
129 View Code Duplication
    public function titleWasChanged(TitleWasChangedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $data = [
132
            'loggable_id' => $event->conversation->getKey(),
133
            'loggable_type' => get_class($event->conversation),
134
            'event_type' => TitleWasChangedEvent::class,
135
            'data' => [
136
                'old' => $event->oldTitle,
137
                'new' => $event->title
138
            ]
139
        ];
140
141
        return $this->create($data);
142
    }
143
144
    /**
145
     * Create the log.
146
     *
147
     * @param array $data The data used to create the log.
148
     *
149
     * @return bool
150
     */
151
    protected function create(array $data): bool
152
    {
153
        if (!isset($data['data'])) {
154
            $data['data'] = [];
155
        }
156
        $log = DiscussLog::create($data);
157
158
        return !(is_null($log));
159
    }
160
}
161