SpamCheckService::isSpamCheckFailed()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
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
namespace DERHANSEN\SfEventMgt\Service;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
15
use DERHANSEN\SfEventMgt\SpamChecks\AbstractSpamCheck;
16
use DERHANSEN\SfEventMgt\SpamChecks\Exceptions\SpamCheckNotFoundException;
17
18
/**
19
 * Service to process spam checks configured in TypoScript
20
 */
21
class SpamCheckService
22
{
23
    protected array $settings = [];
24
    protected array $arguments = [];
25
    protected Registration $registration;
26
    protected int $maxSpamScore = 10;
27
    protected int $checkScore = 0;
28
29
    public function __construct(Registration $registration, array $settings, array $arguments)
30
    {
31
        $this->registration = $registration;
32
        $this->settings = $settings;
33
        $this->arguments = $arguments;
34
35
        if (isset($settings['maxSpamScore'])) {
36
            $this->maxSpamScore = (int)$settings['maxSpamScore'];
37
        }
38
39
        if (empty($settings['checks'] ?? [])) {
40
            $this->settings['checks'] = [];
41
        }
42
    }
43
44
    /**
45
     * Returns, if the spam check failed
46
     *
47
     * @throws SpamCheckNotFoundException
48
     */
49
    public function isSpamCheckFailed(): bool
50
    {
51
        if ((bool)($this->settings['enabled'] ?? false)) {
52
            $this->processSpamChecks();
53
        }
54
55
        return $this->checkScore >= $this->maxSpamScore;
56
    }
57
58
    /**
59
     * Processes all configured spam checks
60
     *
61
     * @throws SpamCheckNotFoundException
62
     */
63
    protected function processSpamChecks(): void
64
    {
65
        foreach ($this->settings['checks'] ?? [] as $checkConfig) {
66
            if (!class_exists($checkConfig['class'] ?? '')) {
67
                throw new SpamCheckNotFoundException('Class ' . $checkConfig['class'] . ' does not exists');
68
            }
69
            $this->processSpamCheck($checkConfig);
70
        }
71
    }
72
73
    /**
74
     * Processes the spam check in the given config
75
     */
76
    protected function processSpamCheck(array $checkConfig): void
77
    {
78
        if (!(bool)($checkConfig['enabled'] ?? false)) {
79
            return;
80
        }
81
        $configuration = $checkConfig['configuration'] ?? [];
82
        /** @var AbstractSpamCheck $spamCheck */
83
        $spamCheck = new $checkConfig['class'](
84
            $this->registration,
85
            $this->settings,
86
            $this->arguments,
87
            $configuration
88
        );
89
        if ($spamCheck->isFailed()) {
90
            $this->checkScore += (int)$checkConfig['increaseScore'];
91
        }
92
    }
93
}
94