Passed
Push — master ( 30d3d2...4e3abd )
by Jesse
01:56
created

StatisticsUpdater::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\EventHandler;
4
5
use function assert;
6
use Stratadox\CardGame\DomainEvent;
7
use Stratadox\CardGame\ReadModel\PageVisitsStatisticsReport;
8
use Stratadox\CardGame\Visiting\BroughtVisitor;
9
use Stratadox\CardGame\Visiting\VisitedPage;
10
11
final class StatisticsUpdater implements EventHandler
12
{
13
    private $statistics;
14
15
    public function __construct(PageVisitsStatisticsReport $statistics)
16
    {
17
        $this->statistics = $statistics;
18
    }
19
20
    public function events(): iterable
21
    {
22
        return [BroughtVisitor::class, VisitedPage::class];
23
    }
24
25
    public function handle(DomainEvent $event): void
26
    {
27
        if ($event instanceof BroughtVisitor) {
28
            $this->newVisitor($event);
29
        } else {
30
            assert($event instanceof VisitedPage);
31
            $this->recurringVisitor($event);
32
        }
33
    }
34
35
    private function newVisitor(BroughtVisitor $event): void
36
    {
37
        $this->statistics->firstVisitFrom($event->aggregateId());
38
    }
39
40
    private function recurringVisitor(VisitedPage $event): void
41
    {
42
        if ($event->isFirstVisit()) {
43
            $this->statistics->firstVisitTo($event->page());
44
        }
45
        $this->statistics->visit($event->page(), $event->source());
46
    }
47
}
48