Completed
Push — master ( 054192...eb3219 )
by Torben
02:56 queued 01:47
created

LinkSpamCheck::isFailed()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 9
nop 0
1
<?php
2
declare(strict_types=1);
3
namespace DERHANSEN\SfEventMgt\SpamChecks;
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * LinksSpamCheck
14
 */
15
class LinkSpamCheck extends AbstractSpamCheck
16
{
17
    /**
18
     * Counts the amount of links in all fields/registration fields and evaluates, if the found amount
19
     * of links is greater than the configured max. amount of links
20
     *
21
     * @return bool
22
     */
23
    public function isFailed(): bool
24
    {
25
        $amountOfLinks = 0;
26
27
        // First check all accessible properties that are strings
28
        foreach ($this->registration->_getProperties() as $value) {
29
            if (!is_string($value)) {
30
                continue;
31
            }
32
33
            $amountOfLinks += $this->getAmountOfLinks($value);
34
        }
35
36
        // Next check all values of possible registration fields
37
        foreach ($this->registration->getFieldValues() as $fieldValue) {
38
            if (!is_string($fieldValue->getValue())) {
39
                continue;
40
            }
41
42
            $amountOfLinks += $this->getAmountOfLinks($fieldValue->getValue());
43
        }
44
45
        return $amountOfLinks > (int)$this->configuration['maxAmountOfLinks'];
46
    }
47
48
    /**
49
     * Returns the amount of links
50
     *
51
     * @param string $value
52
     * @return int
53
     */
54
    private function getAmountOfLinks(string $value): int
55
    {
56
        $pattern = '~[a-z]+://\S+~';
57
        $amount = preg_match_all($pattern, $value, $out);
58
        if ($amount !== false) {
59
            return $amount;
60
        }
61
62
        return 0;
63
    }
64
}
65