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

TicketStatusListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
rs 10
wmc 5
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B preUpdate() 0 19 5
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