Completed
Pull Request — master (#147)
by
unknown
13:27
created

TicketStatusListener::preUpdate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 19
rs 8.8571
1
<?php
2
3
namespace AppBundle\EventListener;
4
5
use AppBundle\Entity\Ticket;
6
use AppBundle\Exception\TicketStatusConflictException;
7
use Doctrine\ORM\Event\PreUpdateEventArgs;
8
9
class TicketStatusListener
10
{
11
    public function preUpdate(PreUpdateEventArgs $args)
12
    {
13
        $ticket = $args->getEntity();
14
15
        if (!$ticket instanceof Ticket && !$args->hasChangedField('status')) {
16
            return;
17
        }
18
        
19
        $oldStatus = $args->getOldValue('status');
20
        $newStatus = $args->getNewValue('status');
21
22
        if (!in_array($newStatus, Ticket::getStatuses())) {
23
            throw new \InvalidArgumentException("Invalid ticket status");
24
        }
25
26
        if ($oldStatus === Ticket::STATUS_PAID) {
27
            throw new TicketStatusConflictException("Invalid status. Ticket already paid.");
28
        }
29
    }
30
}
31