BlockReferralSpam::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\SpamBlocker\Http\Middleware;
2
3
use Arcanedev\SpamBlocker\Contracts\SpamBlocker;
4
use Closure;
5
use Illuminate\Http\Request;
6
7
/**
8
 * Class     BlockReferralSpam
9
 *
10
 * @package  Arcanedev\SpamBlocker\Http\Middleware
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class BlockReferralSpam
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /** @var  \Arcanedev\SpamBlocker\Contracts\SpamBlocker */
21
    protected $blocker;
22
23
    /* -----------------------------------------------------------------
24
     |  Constructor
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * BlockReferralSpam constructor.
30
     *
31
     * @param  \Arcanedev\SpamBlocker\Contracts\SpamBlocker  $blocker
32
     */
33 9
    public function __construct(SpamBlocker $blocker)
34
    {
35 9
        $this->blocker = $blocker;
36 9
    }
37
38
    /* -----------------------------------------------------------------
39
     |  Main Methods
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * Handle an incoming request.
45
     *
46
     * @param  \Illuminate\Http\Request  $request
47
     * @param  \Closure                  $next
48
     *
49
     * @return mixed
50
     */
51 9
    public function handle(Request $request, Closure $next)
52
    {
53 9
        $referer = $request->headers->get('referer');
54
55 9
        return $this->blocker->isBlocked($referer)
0 ignored issues
show
Bug introduced by
It seems like $referer defined by $request->headers->get('referer') on line 53 can also be of type array<integer,string>; however, Arcanedev\SpamBlocker\Co...pamBlocker::isBlocked() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56 6
            ? $this->getBlockedResponse()
57 9
            : $next($request);
58
    }
59
60
    /* -----------------------------------------------------------------
61
     |  Other Methods
62
     | -----------------------------------------------------------------
63
     */
64
65
    /**
66
     * Get the blocked referer's response.
67
     *
68
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
69
     */
70 6
    protected function getBlockedResponse()
71
    {
72 6
        return response('Unauthorized.', 401);
73
    }
74
}
75