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

VoteController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 36
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A vote() 0 34 4
1
<?php
2
3
namespace Coyote\Http\Controllers\Guide;
4
5
use Coyote\Guide;
6
use Coyote\Http\Controllers\Controller;
7
use Coyote\Services\UrlBuilder;
8
use Illuminate\Auth\Access\AuthorizationException;
9
use Illuminate\Http\Request;
10
use Coyote\Services\Stream\Activities\Vote as Stream_Vote;
11
use Coyote\Services\Stream\Objects\Guide as Stream_Guide;
12
13
class VoteController extends Controller
14
{
15
    public function vote(Guide $guide, Request $request)
16
    {
17
        /** @var \Coyote\Guide\Vote $vote */
18
        $vote = $guide->voters()->forUser($this->userId)->first();
19
20
        if (!config('app.debug') && $this->userId === $guide->user_id) {
21
            throw new AuthorizationException('Nie możesz głosować na wpisy swojego autorstwa.');
22
        }
23
24
        $this->transaction(function () use ($vote, $guide, $request) {
25
            if ($vote) {
0 ignored issues
show
introduced by
$vote is of type Coyote\Guide\Vote, thus it always evaluated to true.
Loading history...
26
                $vote->delete();
27
28
                $guide->votes--;
29
            } else {
30
                $vote = $guide->voters()->create(['user_id' => $this->userId, 'ip' => $request->ip()]);
31
                $guide->votes++;
32
            }
33
34
            $target = null;
35
36
37
            $url = UrlBuilder::guide($guide);
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
38
            $object = (new Stream_Guide())->map($guide);
39
40
//                app('reputation.microblog.vote')->map($microblog)->setUrl($url)->setPositive($vote->wasRecentlyCreated)->save();
41
42
43
            $guide->save();
44
45
            // put this to activity stream
46
            stream(Stream_Vote::class, $object, $target);
47
48
            return $vote;
49
        });
50
    }
51
}
52