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

ForbiddenWordSpamScore   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 40
ccs 0
cts 14
cp 0
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
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
}