Passed
Push — master ( 34eee0...6833a3 )
by Adam
16:14 queued 04:06
created

ActivitySubscriber::onCommentSaved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Listeners;
4
5
use Coyote\Events\CommentDeleted;
6
use Coyote\Events\CommentSaved;
7
use Coyote\Events\PostWasDeleted;
8
use Coyote\Events\PostSaved;
9
use Coyote\Events\TopicWasDeleted;
10
use Coyote\Events\TopicWasMoved;
11
use Coyote\Post;
12
use Coyote\Repositories\Contracts\ActivityRepositoryInterface as ActivityRepository;
13
use Illuminate\Contracts\Queue\ShouldQueue;
14
15
class ActivitySubscriber implements ShouldQueue
16
{
17
    /**
18
     * @var ActivityRepository
19
     */
20
    private $activity;
21
22
    /**
23
     * ActivitySubscriber constructor.
24
     * @param ActivityRepository $activity
25
     */
26
    public function __construct(ActivityRepository $activity)
27
    {
28
        $this->activity = $activity;
29
    }
30
31
    /**
32
     * @param PostSaved $event
33
     */
34
    public function onPostSaved(PostSaved $event)
35
    {
36
        $this->activity->updateOrCreate([
0 ignored issues
show
Bug introduced by
The method updateOrCreate() does not exist on Coyote\Repositories\Cont...vityRepositoryInterface. Did you maybe mean update()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $this->activity->/** @scrutinizer ignore-call */ 
37
                         updateOrCreate([

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...
37
            'content_type'  => Post::class,
38
            'content_id'    => $event->post->id,
39
        ], [
40
            'created_at'    => $event->post->created_at,
41
            'user_id'       => $event->post->user_id,
42
            'forum_id'      => $event->post->forum_id,
43
            'topic_id'      => $event->post->topic_id,
44
            'excerpt'       => excerpt($event->post->html),
45
            'user_name'     => $event->post->user_name
46
        ]);
47
    }
48
49
    /**
50
     * @param PostWasDeleted $event
51
     */
52
    public function onPostDeleted(PostWasDeleted $event)
53
    {
54
        $this->activity->where('content_id', $event->post['id'])->where('content_type', Post::class)->delete();
0 ignored issues
show
Bug introduced by
The method where() does not exist on Coyote\Repositories\Cont...vityRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...vityRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $this->activity->/** @scrutinizer ignore-call */ 
55
                         where('content_id', $event->post['id'])->where('content_type', Post::class)->delete();
Loading history...
55
    }
56
57
    public function onTopicDeleted(TopicWasDeleted $event)
58
    {
59
        $this->activity->where('topic_id', $event->topic['id'])->delete();
60
    }
61
62
    public function onTopicMoved(TopicWasMoved $event)
63
    {
64
        $this->activity->where('topic_id', $event->topic['id'])->update(['forum_id' => $event->topic['forum_id']]);
65
    }
66
67
    /**
68
     * @param CommentSaved $event
69
     */
70
    public function onCommentSaved(CommentSaved $event)
71
    {
72
        $this->activity->updateOrCreate([
73
            'content_id'    => $event->comment->id,
74
            'content_type'  => Post\Comment::class,
75
        ], [
76
            'created_at'    => $event->comment->created_at,
77
            'user_id'       => $event->comment->user_id,
78
            'forum_id'      => $event->comment->post->forum_id,
79
            'topic_id'      => $event->comment->post->topic_id,
80
            'excerpt'       => excerpt($event->comment->html)
81
        ]);
82
    }
83
84
    /**
85
     * @param CommentDeleted $event
86
     */
87
    public function onCommentDeleted(CommentDeleted $event)
88
    {
89
        $this->activity->where('content_id', $event->comment['id'])->where('content_type', Post\Comment::class)->delete();
90
    }
91
92
    /**
93
     * Register the listeners for the subscriber.
94
     *
95
     * @param  \Illuminate\Events\Dispatcher  $events
96
     */
97
    public function subscribe($events)
98
    {
99
        $events->listen(
100
            PostSaved::class,
101
            'Coyote\Listeners\ActivitySubscriber@onPostSaved'
102
        );
103
104
        $events->listen(
105
            PostWasDeleted::class,
106
            'Coyote\Listeners\ActivitySubscriber@onPostDeleted'
107
        );
108
109
        $events->listen(
110
            TopicWasDeleted::class,
111
            'Coyote\Listeners\ActivitySubscriber@onTopicDeleted'
112
        );
113
114
        $events->listen(
115
            TopicWasMoved::class,
116
            'Coyote\Listeners\ActivitySubscriber@onTopicMoved'
117
        );
118
119
        $events->listen(
120
            CommentSaved::class,
121
            'Coyote\Listeners\ActivitySubscriber@onCommentSaved'
122
        );
123
124
        $events->listen(
125
            CommentDeleted::class,
126
            'Coyote\Listeners\ActivitySubscriber@onCommentDeleted'
127
        );
128
    }
129
}
130