Completed
Pull Request — master (#141)
by
unknown
14:59
created

TicketStatusListener   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 59
rs 10
wmc 12
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C onFlush() 0 49 11
1
<?php
2
3
namespace AppBundle\EventListener;
4
5
use AppBundle\Entity\CustomerOrder;
6
use AppBundle\Entity\Ticket;
7
use AppBundle\Exception\TicketStatusConflictException;
8
use Doctrine\ORM\Event\OnFlushEventArgs;
9
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
10
11
class TicketStatusListener
12
{
13
    protected $tokenStorage;
14
15
    public function __construct(TokenStorageInterface $tokenStorage)
16
    {
17
        $this->tokenStorage = $tokenStorage;
18
    }
19
20
    public function onFlush(OnFlushEventArgs $args)
21
    {
22
        $em = $args->getEntityManager();
23
        $uow = $em->getUnitOfWork();
24
25
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
26
            if (!$entity instanceof Ticket) {
27
                return;
28
            }
29
30
            if (!key_exists('status', $uow->getEntityChangeSet($entity))) {
31
                return;
32
            }
33
34
            $ticket = $entity;
35
        }
36
37
        $oldStatus = $uow->getEntityChangeSet($ticket)['status'][0];
0 ignored issues
show
Bug introduced by
The variable $ticket does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
38
        $newStatus = $uow->getEntityChangeSet($ticket)['status'][1];
39
40
        if (!in_array($newStatus, Ticket::getStatuses())) {
41
            throw new \InvalidArgumentException("Invalid ticket status");
42
        }
43
44
        if ($oldStatus === Ticket::STATUS_PAID) {
45
            throw new TicketStatusConflictException("Invalid status. Ticket already paid.");
46
        }
47
48
        $user = $this->tokenStorage->getToken()->getUser();
49
50
        /** @var CustomerOrder $order */
51
        $order = $em->getRepository('AppBundle:CustomerOrder')->findOpenedCustomerOrder($user);
52
53
        /**
54
         * Creating order if isn't exist
55
         */
56
        if (!$order) {
57
            $order = new CustomerOrder($user);
58
            $ticketMetadata = $em->getClassMetadata(CustomerOrder::class);
59
            $em->persist($order);
60
            $uow->computeChangeSet($ticketMetadata, $order);
61
        }
62
63
        if ($oldStatus === Ticket::STATUS_FREE && $newStatus === Ticket::STATUS_BOOKED) {
64
            $ticket->setCustomerOrder($order);
65
        } elseif ($oldStatus === Ticket::STATUS_BOOKED && $newStatus === Ticket::STATUS_FREE) {
66
            $ticket->setCustomerOrder(null);
67
        }
68
    }
69
}
70