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

Spammers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 110
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getReferrals() 0 4 1
A setReferrals() 0 6 1
A isSpamRequest() 0 6 1
A isSpam() 0 7 1
A loadReferrals() 0 16 3
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