Notifier   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 0
loc 58
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

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
use function get_class;
12
use function is_int;
13
14
final class Notifier implements NotifierInterface
15
{
16
    private NotifierPolicy $policy;
17
18 2
    /** @var ChannelInterface[] */
19
    private array $channels;
20
21
    public function __construct(
22 2
        NotifierPolicy $policy,
23 2
        ChannelInterface ...$channels,
24 2
    ) {
25
        $this->policy = $policy;
26 2
        $this->channels = $channels;
27
    }
28 2
29
    public function notify(CrawlResult $crawlResult): NotifyResult
30 2
    {
31 2
        $result = new NotifyResult();
32 2
33
        foreach ($this->policy->groupedBySymbol() as $symbol => $policyGroup) {
34 2
            $quote = $crawlResult->getQuote($symbol);
35 1
            $conditionNames = $this->matchConditions($policyGroup, $quote);
36
37
            if (!empty($conditionNames)) {
38
                $result->add($quote, $conditionNames);
39 2
            }
40 1
        }
41
42
        if (!$result->isEmpty()) {
43 2
            $this->sendNotification($result);
44
        }
45
46 2
        return $result;
47
    }
48 2
49
    private function matchConditions(PolicyGroup $policyGroup, Quote $quote): array
50 2
    {
51 2
        $conditionNames = [];
52 1
53
        foreach ($policyGroup->conditions() as $conditionName => $callablePolicy) {
54
            if (!$callablePolicy($quote)) {
55 1
                continue;
56
            }
57
58
            if (is_int($conditionName)) {
59 1
                $conditionName = get_class($callablePolicy);
60
            }
61
62 2
            $conditionNames[] = $conditionName;
63
        }
64
65 1
        return $conditionNames;
66
    }
67 1
68
    private function sendNotification(NotifyResult $result): void
69
    {
70 1
        foreach ($this->channels as $channel) {
71
            $channel->send($result);
72
        }
73
    }
74
}
75