Passed
Push — master ( 89fbbd...77f2dd )
by Adam
12:19
created

PageSubscriber::onWikiSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Listeners;
4
5
use Coyote\Events\JobDeleted;
6
use Coyote\Events\MicroblogDeleted;
7
use Coyote\Events\MicroblogSaved;
8
use Coyote\Events\TopicWasMoved;
9
use Coyote\Events\TopicWasSaved;
10
use Coyote\Events\TopicWasDeleted;
11
use Coyote\Events\ForumWasSaved;
12
use Coyote\Events\ForumWasDeleted;
13
use Coyote\Events\JobWasSaved;
14
use Coyote\Events\WikiWasDeleted;
15
use Coyote\Events\WikiWasSaved;
16
use Coyote\Microblog;
17
use Coyote\Job;
18
use Coyote\Services\UrlBuilder;
19
use Coyote\Topic;
20
use Coyote\Forum;
21
use Coyote\Repositories\Contracts\PageRepositoryInterface as PageRepository;
22
use Coyote\Wiki;
23
use Illuminate\Contracts\Queue\ShouldQueue;
24
use Illuminate\Contracts\Console\Kernel;
25
26
class PageSubscriber implements ShouldQueue
27
{
28
    /**
29
     * Postpone this job to make sure that record was saved in transaction.
30
     *
31
     * @var int
32
     */
33
    public $delay = 10;
34
35
    /**
36
     * @var PageRepository
37
     */
38
    protected $page;
39
40
    /**
41
     * @var Kernel
42
     */
43
    protected $kernel;
44
45
    /**
46
     * @param PageRepository $page
47
     * @param Kernel $kernel
48
     */
49
    public function __construct(PageRepository $page, Kernel $kernel)
50
    {
51
        $this->page = $page;
52
        $this->kernel = $kernel;
53
    }
54
55
    /**
56
     * @param TopicWasSaved $event
57
     */
58
    public function onTopicSave(TopicWasSaved $event)
59
    {
60
        $this->updateTopic($event->topic);
61
    }
62
63
    /**
64
     * @param TopicWasMoved $event
65
     */
66
    public function onTopicMove(TopicWasMoved $event)
67
    {
68
        $this->updateTopic($event->topic);
69
    }
70
71
    /**
72
     * @param Topic $topic
73
     */
74
    private function updateTopic(Topic $topic)
75
    {
76
        $this->purgePageViews();
77
78
        $topic->page()->updateOrCreate([
79
            'content_id'     => $topic->id,
80
            'content_type'   => Topic::class
81
        ], [
82
            'title'          => $topic->title,
83
            'tags'           => $topic->tags->pluck('name'),
84
            'path'           => UrlBuilder::topic($topic),
85
            'allow_sitemap'  => !$topic->forum->access()->exists()
86
        ]);
87
    }
88
89
    /**
90
     * @param TopicWasDeleted $event
91
     */
92
    public function onTopicDelete(TopicWasDeleted $event)
93
    {
94
        $this->page->deleteByContent($event->topic['id'], Topic::class);
95
    }
96
97
    /**
98
     * @param ForumWasSaved $event
99
     */
100
    public function onForumSave(ForumWasSaved $event)
101
    {
102
        $event->forum->page()->updateOrCreate([
103
            'content_id'    => $event->forum->id,
104
            'content_type'  => Forum::class
105
        ], [
106
            'title'         => $event->forum->name,
107
            'path'          => UrlBuilder::forum($event->forum)
108
        ]);
109
    }
110
111
    /**
112
     * @param ForumWasDeleted $event
113
     */
114
    public function onForumDelete(ForumWasDeleted $event)
115
    {
116
        $this->page->deleteByContent($event->forum['id'], Forum::class);
117
    }
118
119
    /**
120
     * @param MicroblogSaved $event
121
     */
122
    public function onMicroblogSave(MicroblogSaved $event)
123
    {
124
        $event->microblog->page()->updateOrCreate([
125
            'content_id'    => $event->microblog->id,
126
            'content_type'  => Microblog::class,
127
        ], [
128
            'title'         => excerpt($event->microblog->html, 28),
129
            'path'          => UrlBuilder::microblog($event->microblog),
130
            'tags'          => $event->microblog->tags->pluck('name')
131
        ]);
132
    }
133
134
    /**
135
     * @param MicroblogDeleted $event
136
     */
137
    public function onMicroblogDelete(MicroblogDeleted $event)
138
    {
139
        $this->page->deleteByContent($event->microblog['id'], Microblog::class);
140
    }
141
142
    /**
143
     * @param JobWasSaved $event
144
     */
145
    public function onJobSave(JobWasSaved $event)
146
    {
147
        $this->purgePageViews();
148
149
        $event->job->page()->updateOrCreate([
150
            'content_id'    => $event->job->id,
151
            'content_type'  => Job::class,
152
        ], [
153
            'title'         => $event->job->title,
154
            'tags'          => $event->job->tags->pluck('name'),
155
            'path'          => UrlBuilder::job($event->job)
156
        ]);
157
    }
158
159
    /**
160
     * @param JobDeleted $event
161
     */
162
    public function onJobDelete(JobDeleted $event)
163
    {
164
        $this->page->deleteByContent($event->job['id'], Job::class);
165
    }
166
167
    /**
168
     * @param WikiWasSaved $event
169
     */
170
    public function onWikiSave(WikiWasSaved $event)
171
    {
172
        $this->purgePageViews();
173
174
        $event->wiki->page()->updateOrCreate([
175
            'content_id'    => $event->wiki->id,
176
            'content_type'  => Wiki::class,
177
        ], [
178
            'title'         => $event->wiki->title,
179
            'path'          => urldecode(UrlBuilder::wiki($event->wiki))
180
        ]);
181
    }
182
183
    /**
184
     * @param WikiWasDeleted $event
185
     */
186
    public function onWikiDelete(WikiWasDeleted $event)
187
    {
188
        $this->page->deleteByContent($event->wiki['id'], Wiki::class);
189
    }
190
191
    /**
192
     * Register the listeners for the subscriber.
193
     *
194
     * @param  \Illuminate\Events\Dispatcher  $events
195
     */
196
    public function subscribe($events)
0 ignored issues
show
Unused Code introduced by
The parameter $events is not used and could be removed. ( Ignorable by Annotation )

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

196
    public function subscribe(/** @scrutinizer ignore-unused */ $events)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
    {
198
        return [
199
            TopicWasSaved::class => 'onTopicSave',
200
            TopicWasMoved::class => 'onTopicMove',
201
            TopicWasDeleted::class => 'onTopicDelete',
202
            ForumWasSaved::class => 'onForumSave',
203
            ForumWasDeleted::class => 'onForumDelete',
204
            MicroblogSaved::class => 'onMicroblogSave',
205
            MicroblogDeleted::class => 'onMicroblogDelete',
206
            JobWasSaved::class => 'onJobSave',
207
            JobDeleted::class => 'onJobDelete',
208
            WikiWasSaved::class => 'onWikiSave',
209
            WikiWasDeleted::class => 'onWikiDelete'
210
        ];
211
    }
212
213
    // before changing page path we MUST save page views that are stored in redis
214
    private function purgePageViews()
215
    {
216
        if (!app()->environment('local', 'dev')) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

216
        if (!app()->/** @scrutinizer ignore-call */ environment('local', 'dev')) {
Loading history...
217
            $this->kernel->call('coyote:counter');
218
        }
219
    }
220
}
221