Visitor::visit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Visiting;
4
5
use DateTimeInterface;
6
use Stratadox\CardGame\Account\AccountId;
7
use Stratadox\CardGame\Account\PlayerAccount;
8
use Stratadox\CardGame\DomainEventRecorder;
9
use Stratadox\CardGame\DomainEventRecording;
10
11
final class Visitor implements DomainEventRecorder
12
{
13
    use DomainEventRecording;
14
15
    private $id;
16
    private $visitedPages = [];
17
18
    public function __construct(VisitorId $id)
19
    {
20
        $this->id = $id;
21
    }
22
23
    public function id(): VisitorId
24
    {
25
        return $this->id;
26
    }
27
28
    public function openAccount(AccountId $playerId): PlayerAccount
29
    {
30
        return PlayerAccount::fromVisitor($this->id, $playerId);
31
    }
32
33
    public function visit(
34
        string $page,
35
        DateTimeInterface $when,
36
        string $source
37
    ): void {
38
        $isFirstVisit = !isset($this->visitedPages[$page]);
39
        if ($isFirstVisit) {
40
            $this->visitedPages[$page] = $when;
41
        }
42
        $this->events[] = new VisitedPage(
43
            $this->id,
44
            $page,
45
            $source,
46
            $isFirstVisit
47
        );
48
    }
49
}
50