Passed
Pull Request — master (#4)
by Chema
02:47
created

FoundMoreNews::findLatestDateTimeFromNews()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.9666
ccs 11
cts 11
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Domain\Notifier\Policy\Condition;
6
7
use Chemaclass\StockTicker\Domain\Notifier\Policy\PolicyConditionInterface;
8
use Chemaclass\StockTicker\Domain\ReadModel\Company;
9
10
final class FoundMoreNews implements PolicyConditionInterface
11
{
12
    public const NEWS = 'NEWS';
13
14
    /**
15
     * @var array<string,string>
16
     * For example ['TickerSymbol' => 'datetime']
17
     */
18
    private static array $cacheOldestDateTimeBySymbol = [];
19
20 1
    public function __construct()
21
    {
22
        // TODO: Add as optional arg: the datetime from where to start looking for news.
23 1
    }
24
25 1
    public function __invoke(Company $company): bool
26
    {
27 1
        $current = $this->findLatestDateTimeFromNews($company);
28 1
        $previous = self::$cacheOldestDateTimeBySymbol[$company->symbol()->toString()] ?? '';
29
30 1
        self::$cacheOldestDateTimeBySymbol[$company->symbol()->toString()] = $current;
31
32 1
        return $current > $previous;
33
    }
34
35 1
    private function findLatestDateTimeFromNews(Company $company): string
36
    {
37 1
        $reduced = array_reduce(
38 1
            (array) $company->info(self::NEWS),
39 1
            static function (?array $carry, array $current): array {
40 1
                if (null === $carry) {
0 ignored issues
show
introduced by
The condition null === $carry is always false.
Loading history...
41 1
                    return $current;
42
                }
43
44 1
                return $carry['datetime'] > $current['datetime']
45 1
                    ? $carry
46 1
                    : $current;
47 1
            }
48
        );
49
50 1
        return $reduced['datetime'];
51
    }
52
}
53