NewsNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use function array_slice;
11
12
final class NewsNormalizer implements NewsNormalizerInterface
13
{
14
    private const NORMALIZED_DATETIME_FORMAT = 'Y-m-d H:i:s';
15
16
    private const DEFAULT_MAX_TEXT_LENGTH_CHARS = 1500;
17
18
    private DateTimeZone $timeZone;
19
20
    private ?int $maxNewsToFetch;
21
22 8
    private int $maxTextLengthChars;
23
24
    public function __construct(
25
        DateTimeZone $timeZone,
26
        ?int $maxNewsToFetch = null,
27 8
        int $maxTextLengthChars = self::DEFAULT_MAX_TEXT_LENGTH_CHARS,
28 8
    ) {
29 8
        $this->timeZone = $timeZone;
30 8
        $this->maxNewsToFetch = $maxNewsToFetch;
31
        $this->maxTextLengthChars = $maxTextLengthChars;
32 6
    }
33
34 6
    public function normalizeDateTime(DateTimeImmutable $dt): string
35
    {
36 6
        $dt->setTimeZone($this->timeZone);
37
38
        return $dt->format(self::NORMALIZED_DATETIME_FORMAT);
39 6
    }
40
41 6
    public function getTimeZoneName(): string
42
    {
43
        return $this->timeZone->getName();
44 6
    }
45
46 6
    public function normalizeText(string $text): string
47 6
    {
48
        if (mb_strlen($text) < $this->maxTextLengthChars) {
49
            return $text;
50
        }
51
52
        return mb_strimwidth($text, 0, $this->maxTextLengthChars, '...');
53 8
    }
54
55 8
    public function limitByMaxToFetch(array $info): array
56 4
    {
57
        if ($this->maxNewsToFetch === null) {
58
            return $info;
59 4
        }
60
61
        return array_slice($info, 0, $this->maxNewsToFetch);
62
    }
63
}
64