ForbiddenWordSpamScore   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A calculate() 0 12 2
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