AccountOpeningProcess   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A handle() 0 14 2
A register() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Account;
4
5
use Stratadox\CardGame\Command;
6
use Stratadox\CardGame\EventBag;
7
use Stratadox\CardGame\CommandHandler;
8
use Stratadox\CardGame\Visiting\AllVisitors;
9
use function assert;
10
11
final class AccountOpeningProcess implements CommandHandler
12
{
13
    private $newIdentity;
14
    private $visitor;
15
    private $playerBase;
16
    private $eventBag;
17
18
    public function __construct(
19
        AccountIdGenerator $identityGenerator,
20
        AllVisitors $visitors,
21
        PlayerBase $playerBase,
22
        EventBag $eventBag
23
    ) {
24
        $this->newIdentity = $identityGenerator;
25
        $this->visitor = $visitors;
26
        $this->playerBase = $playerBase;
27
        $this->eventBag = $eventBag;
28
    }
29
30
    public function handle(Command $command): void
31
    {
32
        assert($command instanceof OpenAnAccount);
33
34
        $visitor = $this->visitor->withId($command->visitorId());
35
        if ($visitor === null) {
36
            $this->eventBag->add(new TriedOpeningAccountForUnknownEntity(
37
                $command->correlationId(),
38
                'Cannot open account for unknown entity'
39
            ));
40
            return;
41
        }
42
43
        $this->register($visitor->openAccount($this->newIdentity->generate()));
44
    }
45
46
    private function register(PlayerAccount $account): void
47
    {
48
        $this->playerBase->add($account);
49
        $this->eventBag->takeFrom($account);
50
    }
51
}
52