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

SpamCheckService::processSpamChecks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
12
use DERHANSEN\SfEventMgt\SpamChecks\AbstractSpamCheck;
13
use DERHANSEN\SfEventMgt\SpamChecks\Exceptions\SpamCheckNotFoundException;
14
15
/**
16
 * Service to process spam checks configured in TypoScript
17
 */
18
class SpamCheckService
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $settings = [];
24
25
    /**
26
     * @var array
27
     */
28
    protected $arguments = [];
29
30
    /**
31
     * @var Registration
32
     */
33
    protected $registration = null;
34
35
    /**
36
     * @var int
37
     */
38
    protected $maxSpamScore = 10;
39
40
    /**
41
     * @var int
42
     */
43
    protected $checkScore = 0;
44
45
    /**
46
     * SpamCheckService constructor.
47
     *
48
     * @param Registration $registration
49
     * @param array $settings
50
     * @param array $arguments
51
     */
52
    public function __construct(Registration $registration, array $settings, array $arguments)
53
    {
54
        $this->registration = $registration;
55
        $this->settings = $settings;
56
        $this->arguments = $arguments;
57
58
        if (isset($settings['maxSpamScore'])) {
59
            $this->maxSpamScore = (int)$settings['maxSpamScore'];
60
        }
61
62
        if (!isset($settings['checks']) || $settings['checks'] === null) {
63
            $this->settings['checks'] = [];
64
        }
65
    }
66
67
    /**
68
     * Returns, if the spam check failed
69
     *
70
     * @throws SpamCheckNotFoundException
71
     * @return bool
72
     */
73
    public function isSpamCheckFailed(): bool
74
    {
75
        if ((bool)$this->settings['enabled']) {
76
            $this->processSpamChecks();
77
        }
78
79
        return $this->checkScore >= $this->maxSpamScore;
80
    }
81
82
    /**
83
     * Processes all configured spam checks
84
     *
85
     * @throws SpamCheckNotFoundException
86
     * @return void
87
     */
88
    protected function processSpamChecks()
89
    {
90
        foreach ($this->settings['checks'] as $checkConfig) {
91
            if (!class_exists($checkConfig['class'])) {
92
                throw new SpamCheckNotFoundException('Class ' . $checkConfig['class'] . ' does not exists');
93
            }
94
            $this->processSpamCheck($checkConfig);
95
        }
96
    }
97
98
    /**
99
     * Prococesses the spam check in the given config
100
     *
101
     * @param array $checkConfig
102
     * @return void
103
     */
104
    protected function processSpamCheck(array $checkConfig)
105
    {
106
        if (!(bool)$checkConfig['enabled']) {
107
            return;
108
        }
109
        $configuration = $checkConfig['configuration'] ?? [];
110
        /** @var AbstractSpamCheck $spamCheck */
111
        $spamCheck = new $checkConfig['class']($this->registration, $this->settings, $this->arguments,
112
            $configuration);
113
        if ($spamCheck->isFailed()) {
114
            $this->checkScore += (int)$checkConfig['increaseScore'];
115
        }
116
    }
117
}
118