Completed
Pull Request — master (#17)
by Frederik
13:03
created

ForbiddenWordSpamScore::calculate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Genkgo\Mail\Protocol\Smtp\SpamScore;
4
5
use Genkgo\Mail\MessageInterface;
6
use Genkgo\Mail\Protocol\Smtp\SpamScoreInterface;
7
8
final class ForbiddenWordSpamScore implements SpamScoreInterface
9
{
10
11
    /**
12
     * @var array
13
     */
14
    private $words;
15
    /**
16
     * @var int
17
     */
18
    private $pointsPerMatchedWord;
19
20
    /**
21
     * ForbiddenWordSpamCheck constructor.
22
     * @param array $words
23
     * @param int $pointsPerMatchedWord
24
     */
25
    public function __construct(array $words, int $pointsPerMatchedWord)
26
    {
27
        $this->words = $words;
28
        $this->pointsPerMatchedWord = $pointsPerMatchedWord;
29
    }
30
31
    /**
32
     * @param MessageInterface $message
33
     * @return int
34
     */
35
    public function calculate(MessageInterface $message): int
36
    {
37
        $messageBody = strtolower((string)$message);
38
39
        $score = 0;
40
41
        foreach ($this->words as $word) {
42
            $score += $this->pointsPerMatchedWord * substr_count($messageBody, strtolower($word));
43
        }
44
45
        return $score;
46
    }
47
}