Conditions | 4 |
Paths | 2 |
Total Lines | 34 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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) { |
||
|
|||
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); |
||
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 | }); |
||
52 |