Passed
Pull Request — master (#6)
by Chris
14:09 queued 05:19
created

ImportPurchase::handleTicketCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Checkin\Domain\ProcessManager;
4
5
use Carnage\Cqrs\MessageHandler\AbstractMethodNameMessageHandler;
6
use Carnage\Cqrs\Persistence\EventStore\NotFoundException;
7
use Carnage\Cqrs\Persistence\Repository\RepositoryInterface;
8
use ConferenceTools\Checkin\Domain\Event\Delegate\DelegateRegistered;
9
use ConferenceTools\Checkin\Domain\Event\Purchase\TicketAssigned;
10
use ConferenceTools\Checkin\Domain\Event\Purchase\TicketCreated;
11
use ConferenceTools\Checkin\Domain\Event\Purchase\TicketPurchasePaid;
12
use ConferenceTools\Checkin\Domain\Process\ImportPurchase as ImportPurchaseProcess;
13
14
class ImportPurchase extends AbstractMethodNameMessageHandler
15
{
16
    private $repository;
17
18 7
    public function __construct(RepositoryInterface $repository)
19
    {
20 7
        $this->repository = $repository;
21 7
    }
22
23 3
    protected function handleTicketCreated(TicketCreated $event)
24
    {
25 3
        $process = $this->loadProcess($event->getTicket()->getPurchaseId());
26 3
        $process->ticketCreated($event);
27 3
        $this->repository->save($process);
28 3
    }
29
30 4
    protected function handleTicketAssigned(TicketAssigned $event)
31
    {
32 4
        $process = $this->loadProcess($event->getTicket()->getPurchaseId());
33 4
        $process->ticketAssigned($event);
34 4
        $this->repository->save($process);
35 4
    }
36
37 5
    protected function handleTicketPurchasePaid(TicketPurchasePaid $event)
38
    {
39 5
        $process = $this->loadProcess($event->getPurchaseId());
40 5
        $process->ticketPurchasePaid($event);
41 5
        $this->repository->save($process);
42 5
    }
43
44 2
    protected function handleDelegateRegistered(DelegateRegistered $event)
45
    {
46 2
        $process = $this->loadProcess($event->getTicket()->getPurchaseId());
47 2
        $process->delegateRegistered($event);
48 2
        $this->repository->save($process);
49 2
    }
50
51 7
    private function loadProcess($id): ImportPurchaseProcess
52
    {
53
        try {
54 7
            $process = $this->repository->load($id);
55 7
        } catch (NotFoundException $e) {
56 7
            $process = ImportPurchaseProcess::withId($id);
57
        }
58
59 7
        return $process;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $process could return the type Carnage\Cqrs\Aggregate\AggregateInterface which includes types incompatible with the type-hinted return ConferenceTools\Checkin\...\Process\ImportPurchase. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
}