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

VoteController::vote()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
nc 2
nop 2
dl 0
loc 34
rs 9.7333
c 1
b 0
f 0
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