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

FoundMoreNews::findLatestPubtimeFromNews()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 1
nop 1
dl 0
loc 16
ccs 0
cts 6
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
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
    /**
13
     * @var array<string,string>
14
     * For example ['TickerSymbol' => 'datetime']
15
     */
16
    private static array $cacheOldestDateTimeBySymbol = [];
17
18
    private string $companyKey;
19
20 1
    public function __construct(string $companyKey)
21
    {
22 1
        $this->companyKey = $companyKey;
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($this->companyKey),
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