Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

SubscribeController::topic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers;
4
5
use Coyote\Events\TopicSaved;
6
use Coyote\Guide;
7
use Coyote\Microblog;
8
use Coyote\Post;
9
use Coyote\Topic;
10
11
class SubscribeController extends Controller
12
{
13
    public function guide(Guide $guide)
14
    {
15
        return $this->toggle($guide);
16
    }
17
18
    public function microblog(Microblog $microblog)
19
    {
20
        return $this->toggle($microblog);
21
    }
22
23
    public function post(Post $post)
24
    {
25
        return $this->toggle($post);
26
    }
27
28
    public function topic(Topic $topic)
29
    {
30
        $count = $this->toggle($topic);
31
32
        event(new TopicSaved($topic));
33
34
        return $count;
35
    }
36
37
    private function toggle(Guide | Microblog | Topic | Post $model)
38
    {
39
        $subscriber = $model->subscribers()->forUser($this->userId)->first();
40
41
        if ($subscriber) {
42
            $subscriber->delete();
43
        } else {
44
            $model->subscribers()->create(['user_id' => $this->userId]);
45
        }
46
47
        return response($model->subscribers()->count());
48
    }
49
}
50