Passed
Push — master ( c541cb...64a56e )
by Chris
33s
created

TicketCounts::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Projection;
4
5
use Carnage\Cqrs\Event\Projection\ResettableInterface;
6
use Carnage\Cqrs\MessageHandler\AbstractMethodNameMessageHandler;
7
use Doctrine\ORM\EntityManagerInterface;
8
use ConferenceTools\Tickets\Domain\Event\Ticket\TicketReleased;
9
use ConferenceTools\Tickets\Domain\Event\Ticket\TicketReserved;
10
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter;
11
use ConferenceTools\Tickets\Domain\Service\Configuration;
12
use ConferenceTools\Tickets\Domain\ValueObject\Money;
13
use ConferenceTools\Tickets\Domain\ValueObject\Price;
14
use ConferenceTools\Tickets\Domain\ValueObject\TaxRate;
15
use ConferenceTools\Tickets\Domain\ValueObject\TicketType;
16
17
class TicketCounts extends AbstractMethodNameMessageHandler implements ResettableInterface
18
{
19
    private $em;
20
21
    /**
22
     * @var Configuration
23
     */
24
    private $ticketConfig;
25
26
    public function __construct(EntityManagerInterface $em, Configuration $ticketConfig)
27
    {
28
        $this->em = $em;
29
        $this->ticketConfig = $ticketConfig;
30
    }
31
32
    public function reset()
33
    {
34
        $em = $this->em;
35
36
        $q = $em->createQuery(sprintf('delete from %s', TicketCounter::class));
37
        $q->execute();
38
39
        foreach ($this->ticketConfig->getTicketTypes() as $handle => $ticketType) {
40
            $entity = new TicketCounter(
41
                $ticketType,
42
                $this->ticketConfig->getAvailableTickets($handle)
43
            );
44
            $em->persist($entity);
45
        }
46
47
        $em->flush();
48
    }
49
50 View Code Duplication
    protected function handleTicketReserved(TicketReserved $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $counter = $this->em->getRepository(TicketCounter::class)->findOneBy(
53
            ['ticketType.identifier' => $event->getTicketType()->getIdentifier()]
54
        );
55
        $counter->ticketsReserved(1);
56
        $this->em->flush();
57
    }
58
    
59 View Code Duplication
    protected function handleTicketReleased(TicketReleased $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        $counter = $this->em->getRepository(TicketCounter::class)->findOneBy(
62
            ['ticketType.identifier' => $event->getTicketType()->getIdentifier()]
63
        );
64
65
        $counter->ticketsReleased(1);
66
        $this->em->flush();
67
    }
68
}