Completed
Push — master ( 389031...756eed )
by ARCANEDEV
9s
created

Spammers::loadReferrals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 3.054
1
<?php namespace Arcanesoft\Seo\Helpers;
2
3
use Illuminate\Http\Request;
4
5
/**
6
 * Class     Spammers
7
 *
8
 * @package  Arcanesoft\Seo\Helpers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Spammers
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * List of referrer spammers.
19
     *
20
     * @var array
21
     */
22
    protected $referrals = [];
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Constructor
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Spammers constructor.
30
     *
31
     * @param  array  $configs
32
     */
33 8
    public function __construct(array $configs)
34
    {
35 8
        $this->loadReferrals($configs);
36 8
    }
37
38
    /* ------------------------------------------------------------------------------------------------
39
     |  Getters & Setters
40
     | ------------------------------------------------------------------------------------------------
41
     */
42
    /**
43
     * Set the list of spammers.
44
     *
45
     * @return array
46
     */
47 8
    public function getReferrals()
48
    {
49 8
        return $this->referrals;
50
    }
51
52
    /**
53
     * Set the list of spammers.
54
     *
55
     * @param  array  $referrals
56
     *
57
     * @return $this
58
     */
59 8
    public function setReferrals(array $referrals)
60
    {
61 8
        $this->referrals = $referrals;
62
63 8
        return $this;
64
    }
65
66
    /* ------------------------------------------------------------------------------------------------
67
     |  Check Functions
68
     | ------------------------------------------------------------------------------------------------
69
     */
70
    /**
71
     * Check if a request is a referer spam.
72
     *
73
     * @param  \Illuminate\Http\Request  $request
74
     *
75
     * @return bool
76
     */
77 8
    public function isSpamRequest(Request $request)
78
    {
79 8
        $referer = parse_url($request->headers->get('referer'), PHP_URL_HOST);
80
81 8
        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 79 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...
82
    }
83
84
    /**
85
     * Check if referer is a spam.
86
     *
87
     * @param  string  $referer
88
     *
89
     * @return bool
90
     */
91 8
    public function isSpam($referer)
92
    {
93 8
        return in_array(
94 8
            preg_replace('/(www\.)/i', '', $referer),
95 8
            $this->getReferrals()
96
        );
97
    }
98
99
    /**
100
     * Load the referral spammers.
101
     *
102
     * @param  array  $configs
103
     */
104 8
    private function loadReferrals(array $configs)
105
    {
106 8
        $referrals = [];
107
108 8
        switch (array_get($configs, 'driver', 'array')) {
109 8
            case 'database':
110
                $referrals = [];
111
                break;
112
113 8
            case 'array':
114 8
                $referrals = array_get($configs, 'sources.array', []);
115 8
                break;
116
        }
117
118 8
        $this->setReferrals($referrals);
119 8
    }
120
}
121