ForbiddenWordSpamScore::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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