Passed
Push — master ( f2dd02...330c2d )
by Adam
12:12
created

PollRequest::failedAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Requests;
4
5
use Coyote\Poll;
6
use Illuminate\Auth\Access\AuthorizationException;
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class PollRequest extends FormRequest
10
{
11
    private Poll $poll;
12
13
    /**
14
     * Determine if the user is authorized to make this request.
15
     *
16
     * @return bool
17
     */
18
    public function authorize()
19
    {
20
        $this->poll = $this->route('poll');
21
22
        if ($this->user() === null || $this->isFraudDetected()) {
23
            return false;
24
        }
25
26
        return true;
27
    }
28
29
    /**
30
     * Get the validation rules that apply to the request.
31
     *
32
     * @return array
33
     */
34
    public function rules()
35
    {
36
        return [
37
            'items' => 'required|array|max:' . $this->poll->max_items,
38
            'items.*' => 'required|integer|in:' . $this->poll->items()->pluck('id')->implode(',')
39
        ];
40
    }
41
42
    protected function failedAuthorization()
43
    {
44
        throw new AuthorizationException('Brak uprawnień do oddania głosu w ankiecie.');
45
    }
46
47
    private function isFraudDetected(): bool
48
    {
49
        /** @var \Illuminate\Support\Collection $collection */
50
        $collection = $this->poll->votes;
51
52
        $userId = $this->user()->id;
53
        $ip = $this->getClientIp();
54
        $fingerprint = request()->fingerprint;
55
56
        return $collection->contains('user_id', $userId) || $collection->contains('ip', $ip) || $collection->contains('fingerprint', $fingerprint);
57
    }
58
}
59