Passed
Push — master ( 4327f5...645913 )
by Chris
03:00
created

ImportPurchase::ticketAssigned()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 9.4285
ccs 8
cts 8
cp 1
crap 2
1
<?php
2
3
namespace ConferenceTools\Checkin\Domain\Process;
4
5
use Carnage\Cqrs\Aggregate\AbstractAggregate;
6
use Carnage\Cqrs\Process\NewProcessInterface;
7
use ConferenceTools\Checkin\Domain\Command\Delegate\RegisterDelegate;
8
use ConferenceTools\Checkin\Domain\Command\Delegate\UpdateDelegateInformation;
9
use ConferenceTools\Checkin\Domain\Event\Delegate\DelegateRegistered;
10
use ConferenceTools\Checkin\Domain\Event\Purchase\TicketAssigned;
11
use ConferenceTools\Checkin\Domain\Event\Purchase\TicketPurchasePaid;
12
13
class ImportPurchase extends AbstractAggregate implements NewProcessInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $purchaseId;
19
20
    /**
21
     * @var bool
22
     */
23
    private $paid = false;
24
25
    /**
26
     * @var TicketAssigned[]
27
     */
28
    private $tickets = [];
29
30
    /**
31
     * @var string
32
     */
33
    private $purchaserEmail;
34
35
    /**
36
     * @var array
37
     */
38
    private $delegates;
39
40 4
    public static function withId(string $id)
41
    {
42 4
        $instance = new self;
43 4
        $instance->purchaseId = $id;
44 4
        return $instance;
45
    }
46
47 4
    public function getId()
48
    {
49 4
        return $this->purchaseId;
50
    }
51
52
    /*
53
     * @TODO handle events arriving out of order.
54
     */
55 3
    public function ticketAssigned(TicketAssigned $event)
56
    {
57 3
        if (!$this->paid) {
58 3
            $this->apply($event);
59
        } else {
60 1
            $ticket = $event->getTicket();
61 1
            $this->apply(
62 1
                new UpdateDelegateInformation(
63 1
                    $this->delegates[$ticket->getPurchaseId()][$ticket->getTicketId()],
64 1
                    $event->getDelegateInfo()
65
                )
66
            );
67
        }
68 3
    }
69
70 1
    protected function applyUpdateDelegateInformation(UpdateDelegateInformation $command)
71
    {
72
73 1
    }
74
75 3
    protected function applyTicketAssigned(TicketAssigned $event)
76
    {
77 3
        $this->purchaseId = $event->getTicket()->getPurchaseId();
78 3
        $this->tickets[] = $event;
79 3
    }
80
81 3
    public function ticketPurchasePaid(TicketPurchasePaid $event)
82
    {
83 3
        $this->apply($event);
84
85 3
        foreach ($this->tickets as $ticket) {
86 2
            $this->apply(new RegisterDelegate($ticket->getDelegateInfo(), $ticket->getTicket(), $this->purchaserEmail));
87
        }
88 3
    }
89
90 3
    protected function applyTicketPurchasePaid(TicketPurchasePaid $event)
91
    {
92 3
        $this->purchaseId = $event->getPurchaseId();
93 3
        $this->paid = true;
94 3
        $this->purchaserEmail = $event->getPurchaserEmail();
95 3
    }
96
97 2
    protected function applyRegisterDelegate(RegisterDelegate $command)
98
    {
99 2
        array_shift($this->tickets);
100 2
    }
101
102 1
    public function delegateRegistered(DelegateRegistered $event)
103
    {
104 1
        $this->apply($event);
105 1
    }
106
107 1
    protected function applyDelegateRegistered(DelegateRegistered $event)
108
    {
109 1
        $ticket = $event->getTicket();
110 1
        $this->delegates[$ticket->getPurchaseId()][$ticket->getTicketId()] = $event->getDelegateId();
111
    }
112
}