Passed
Push — master ( 204398...443e32 )
by Chema
01:50 queued 12s
created

NewsNormalizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
c 0
b 0
f 0
dl 0
loc 50
rs 10
ccs 17
cts 18
cp 0.9444

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getTimeZoneName() 0 3 1
A limitByMaxToFetch() 0 7 2
A normalizeDateTime() 0 5 1
A normalizeText() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Domain\Crawler\Site\Shared;
6
7
use DateTimeImmutable;
8
use DateTimeZone;
9
10
final class NewsNormalizer implements NewsNormalizerInterface
11
{
12
    private const NORMALIZED_DATETIME_FORMAT = 'Y-m-d H:i:s';
13
14
    private const DEFAULT_MAX_TEXT_LENGTH_CHARS = 1500;
15
16
    private DateTimeZone $timeZone;
17
18
    private ?int $maxNewsToFetch;
19
20
    private int $maxTextLengthChars;
21
22 8
    public function __construct(
23
        DateTimeZone $timeZone,
24
        ?int $maxNewsToFetch = null,
25
        int $maxTextLengthChars = self::DEFAULT_MAX_TEXT_LENGTH_CHARS
26
    ) {
27 8
        $this->timeZone = $timeZone;
28 8
        $this->maxNewsToFetch = $maxNewsToFetch;
29 8
        $this->maxTextLengthChars = $maxTextLengthChars;
30 8
    }
31
32 6
    public function normalizeDateTime(DateTimeImmutable $dt): string
33
    {
34 6
        $dt->setTimeZone($this->timeZone);
35
36 6
        return $dt->format(self::NORMALIZED_DATETIME_FORMAT);
37
    }
38
39 6
    public function getTimeZoneName(): string
40
    {
41 6
        return $this->timeZone->getName();
42
    }
43
44 6
    public function normalizeText(string $text): string
45
    {
46 6
        if (mb_strlen($text) < $this->maxTextLengthChars) {
47 6
            return $text;
48
        }
49
50
        return mb_strimwidth($text, 0, $this->maxTextLengthChars, '...');
51
    }
52
53 8
    public function limitByMaxToFetch(array $info): array
54
    {
55 8
        if (null === $this->maxNewsToFetch) {
56 4
            return $info;
57
        }
58
59 4
        return array_slice($info, 0, $this->maxNewsToFetch);
60
    }
61
}
62