VisitationProcess::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.9
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Visiting;
4
5
use function assert;
6
use Stratadox\CardGame\Command;
7
use Stratadox\CardGame\EventBag;
8
use Stratadox\CardGame\CommandHandler;
9
use Stratadox\Clock\Clock;
10
11
final class VisitationProcess implements CommandHandler
12
{
13
    private $allVisitors;
14
    private $redirectSources;
15
    private $clock;
16
    private $eventBag;
17
18
    public function __construct(
19
        AllVisitors $allVisitors,
20
        RedirectSources $redirectSources,
21
        Clock $clock,
22
        EventBag $eventBag
23
    ) {
24
        $this->allVisitors = $allVisitors;
25
        $this->redirectSources = $redirectSources;
26
        $this->clock = $clock;
27
        $this->eventBag = $eventBag;
28
    }
29
30
    public function handle(Command $visit): void
31
    {
32
        assert($visit instanceof Visit);
33
34
        $source = $this->redirectSources->named($visit->redirectSource());
35
        $visitorId = $visit->visitorId();
36
        $visitor = $this->allVisitors->withId($visitorId);
37
38
        if ($visitor === null) {
39
            $source->bring($visitorId);
40
            $visitor = $source->visitorWithId($visitorId);
41
            $this->allVisitors->add($visitor);
42
        }
43
44
        $visitor->visit($visit->whichPage(), $this->clock->now(), $source->id());
45
46
        $this->eventBag->takeFrom($source);
47
        $this->eventBag->takeFrom($visitor);
48
    }
49
}
50