Passed
Push — master ( c0f32c...e879f4 )
by Chema
04:17 queued 41s
created

Notifier   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 11
eloc 24
c 0
b 0
f 0
dl 0
loc 58
ccs 24
cts 26
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A matchConditions() 0 17 4
A notify() 0 18 4
A __construct() 0 6 1
A sendNotification() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Domain\Notifier;
6
7
use Chemaclass\StockTicker\Domain\Crawler\CrawlResult;
8
use Chemaclass\StockTicker\Domain\Notifier\Policy\PolicyGroup;
9
use Chemaclass\StockTicker\Domain\WriteModel\Quote;
10
11
final class Notifier implements NotifierInterface
12
{
13
    private NotifierPolicy $policy;
14
15
    /** @var ChannelInterface[] */
16
    private array $channels;
17
18 2
    public function __construct(
19
        NotifierPolicy $policy,
20
        ChannelInterface ...$channels
21
    ) {
22 2
        $this->policy = $policy;
23 2
        $this->channels = $channels;
24 2
    }
25
26 2
    public function notify(CrawlResult $crawlResult): NotifyResult
27
    {
28 2
        $result = new NotifyResult();
29
30 2
        foreach ($this->policy->groupedBySymbol() as $symbol => $policyGroup) {
31 2
            $quote = $crawlResult->getQuote($symbol);
32 2
            $conditionNames = $this->matchConditions($policyGroup, $quote);
33
34 2
            if (!empty($conditionNames)) {
35 1
                $result->add($quote, $conditionNames);
36
            }
37
        }
38
39 2
        if (!$result->isEmpty()) {
40 1
            $this->sendNotification($result);
41
        }
42
43 2
        return $result;
44
    }
45
46 2
    private function matchConditions(PolicyGroup $policyGroup, Quote $quote): array
47
    {
48 2
        $conditionNames = [];
49
50 2
        foreach ($policyGroup->conditions() as $conditionName => $callablePolicy) {
51 2
            if (!$callablePolicy($quote)) {
52 1
                continue;
53
            }
54
55 1
            if (is_int($conditionName)) {
56
                $conditionName = get_class($callablePolicy);
57
            }
58
59 1
            $conditionNames[] = $conditionName;
60
        }
61
62 2
        return $conditionNames;
63
    }
64
65 1
    private function sendNotification(NotifyResult $result): void
66
    {
67 1
        foreach ($this->channels as $channel) {
68
            $channel->send($result);
69
        }
70 1
    }
71
}
72