Completed
Push — master ( 756eed...5aa9a0 )
by ARCANEDEV
03:52
created

Spammers::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3.072
1
<?php namespace Arcanesoft\Seo\Helpers;
2
3
use Illuminate\Http\Request;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     Spammers
8
 *
9
 * @package  Arcanesoft\Seo\Helpers
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Spammers
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * List of referrer spammers.
20
     *
21
     * @var array
22
     */
23
    protected $referrals = [];
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Constructor
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Spammers constructor.
31
     *
32
     * @param  array  $configs
33 8
     */
34
    public function __construct(array $configs)
35 8
    {
36 8
        $this->load($configs);
37
    }
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Getters & Setters
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Set the list of spammers.
45
     *
46
     * @return array
47 8
     */
48
    public function getReferrals()
49 8
    {
50
        return $this->referrals;
51
    }
52
53
    /**
54
     * Set the list of spammers.
55
     *
56
     * @param  array  $referrals
57
     *
58
     * @return $this
59 8
     */
60
    public function setReferrals(array $referrals)
61 8
    {
62
        $this->referrals = $referrals;
63 8
64
        return $this;
65
    }
66
67
    /* ------------------------------------------------------------------------------------------------
68
     |  Check Functions
69
     | ------------------------------------------------------------------------------------------------
70
     */
71
    /**
72
     * Check if a request is a referer spam.
73
     *
74
     * @param  \Illuminate\Http\Request  $request
75
     *
76
     * @return bool
77 8
     */
78
    public function isSpamRequest(Request $request)
79 8
    {
80
        $referer = parse_url($request->headers->get('referer'), PHP_URL_HOST);
81 8
82
        return $this->isSpam($referer);
0 ignored issues
show
Security Bug introduced by
It seems like $referer defined by parse_url($request->head...eferer'), PHP_URL_HOST) on line 80 can also be of type false; however, Arcanesoft\Seo\Helpers\Spammers::isSpam() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
83
    }
84
85
    /**
86
     * Check if referer is a spam.
87
     *
88
     * @param  string  $referer
89
     *
90
     * @return bool
91 8
     */
92
    public function isSpam($referer)
93 8
    {
94 8
        return in_array(
95 8
            preg_replace('/(www\.)/i', '', $referer),
96
            $this->getReferrals()
97
        );
98
    }
99
100
    /**
101
     * Load the referral spammers.
102
     *
103
     * @param  array  $configs
104 8
     */
105
    public function load(array $configs)
106 8
    {
107
        $referrals = [];
108 8
109 8
        switch (Arr::get($configs, 'driver', 'array')) {
110
            case 'database':
111
                $referrals = [];
112
                break;
113 8
114 8
            case 'array':
115 8
                $referrals = Arr::get($configs, 'sources.array', []);
116
                break;
117
        }
118 8
119 8
        $this->setReferrals($referrals);
120
121
        return $this;
122
    }
123
}
124