Completed
Pull Request — master (#141)
by
unknown
12:54
created

TicketStatusListener::checkTicketStatusUpdate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 8.8571
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 AppBundle\Repository\CustomerOrderRepository;
9
use Doctrine\ORM\Event\OnFlushEventArgs;
10
use Doctrine\ORM\UnitOfWork;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
13
class TicketStatusListener
14
{
15
    protected $tokenStorage;
16
17
    public function __construct(TokenStorageInterface $tokenStorage)
18
    {
19
        $this->tokenStorage = $tokenStorage;
20
    }
21
22
    public function onFlush(OnFlushEventArgs $args)
23
    {
24
        $em = $args->getEntityManager();
25
        $uow = $em->getUnitOfWork();
26
27
        if (!$ticket = $this->checkTicketStatusUpdate($uow)) {
28
            return;
29
        }
30
31
        $oldStatus = $uow->getEntityChangeSet($ticket)['status'][0];
32
        $newStatus = $uow->getEntityChangeSet($ticket)['status'][1];
33
34
        if (!in_array($newStatus, Ticket::getStatuses())) {
35
            throw new \InvalidArgumentException("Invalid ticket status");
36
        }
37
38
        if ($oldStatus === Ticket::STATUS_PAID) {
39
            throw new TicketStatusConflictException("Invalid status. Ticket already paid.");
40
        }
41
42
        if ($oldStatus === Ticket::STATUS_OFFLINE) {
43
            return;
44
        }
45
46
        //$user = $this->tokenStorage->getToken()->getUser();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
        $user = $em->getRepository('AppBundle:Customer')->find(1);
48
        /** @var CustomerOrderRepository $customerOrderRepository */
49
        $customerOrderRepository = $em->getRepository('AppBundle:CustomerOrder');
50
        $order = $customerOrderRepository->findLastOpenOrder($user);
0 ignored issues
show
Documentation introduced by
$user is of type object|null, but the function expects a object<AppBundle\Entity\Customer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
52
        /**
53
         * Creating order if isn't exist
54
         */
55
        if (!$order) {
56
            $order = new CustomerOrder($user);
0 ignored issues
show
Documentation introduced by
$user is of type object|null, but the function expects a object<AppBundle\Entity\Customer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
            $ticketMetadata = $em->getClassMetadata(CustomerOrder::class);
58
            $em->persist($order);
59
            $uow->computeChangeSet($ticketMetadata, $order);
60
        }
61
62
        /**
63
         * Set an order to a ticket
64
         *
65
         */
66
        if ($oldStatus === Ticket::STATUS_FREE && $newStatus === Ticket::STATUS_BOOKED) {
67
            $ticket->setCustomerOrder($order);
68
            /**
69
             * Unset an order from a ticket
70
             */
71
        } elseif ($oldStatus === Ticket::STATUS_BOOKED && $newStatus === Ticket::STATUS_FREE) {
72
            $ticket->setCustomerOrder(null);
73
        }
74
    }
75
76
    /**
77
     * Checks if ticket status was updated
78
     *
79
     * @param UnitOfWork $uow
80
     * @return Ticket|false
81
     */
82
    private function checkTicketStatusUpdate(UnitOfWork $uow)
83
    {
84
        if (!$uow->getScheduledEntityUpdates()) {
85
            return false;
86
        }
87
88
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
89
            if (!$entity instanceof Ticket && !key_exists('status', $uow->getEntityChangeSet($entity))) {
90
                return false;
91
            }
92
            return $entity;
93
        }
94
    }
95
}
96