Completed
Push — master ( 5bb9e4...33b9a4 )
by Serhii
09:16 queued 09:08
created

TicketRepository::enableTicketsForSale()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
nc 6
nop 1
dl 0
loc 35
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Repository;
4
5
use AppBundle\Entity\PerformanceEvent;
6
use AppBundle\Entity\Seat;
7
use AppBundle\Entity\Ticket;
8
use AppBundle\Exception\Ticket\NotRemovableSetException;
9
10
class TicketRepository extends AbstractRepository
11
{
12
13
    /**
14
     * @param PerformanceEvent $performanceEvent
15
     *
16
     * @return Ticket[]
17
     * @throws NotRemovableSetException
18
     */
19
    public function getRemovableTicketSet(PerformanceEvent $performanceEvent): array
20
    {
21
        $ticket = $this->findOneBy([
22
            'performanceEvent' => $performanceEvent,
23
            'status' => [Ticket::STATUS_BOOKED, Ticket::STATUS_PAID]
24
        ]);
25
26
        if (!empty($ticket)) {
27
            throw new NotRemovableSetException(
28
                sprintf(
29
                    'Impossible to remove tickets for PerformanceEvent: %s.',
30
                    $performanceEvent
31
                )
32
            );
33
        }
34
35
        $tickets = $this->findBy([
36
            'performanceEvent' => $performanceEvent
37
        ]);
38
39
        return $tickets;
40
    }
41
42
    /**
43
     * @param PerformanceEvent $performanceEvent
44
     *
45
     * @return bool
46
     */
47
    public function isGeneratedSet(PerformanceEvent $performanceEvent)
48
    {
49
        $ticket = $this->findOneBy([
50
            'performanceEvent' => $performanceEvent
51
        ]);
52
        return !empty($ticket);
53
    }
54
55
    /**
56
     * @param Ticket[] $tickets
57
     */
58
    public function batchSave(array $tickets)
59
    {
60
        foreach ($tickets as $ticket) {
61
            $this->save($ticket, false);
62
        }
63
        $this->getEntityManager()->flush();
64
    }
65
66
    /**
67
     * @param Ticket[] $tickets
68
     */
69
    public function batchRemove(array $tickets)
70
    {
71
        foreach ($tickets as $ticket) {
72
            $this->remove($ticket, false);
73
        }
74
        $this->getEntityManager()->flush();
75
    }
76
77
    /**
78
     * Change Status in Ticket
79
     * form STATUS_OFFLINE to STATUS_FREE if Ticket is in RowsForSale
80
     * and vice versa
81
     *
82
     * @param PerformanceEvent $performanceEvent
83
     * @return int
84
     */
85
    public function enableTicketsForSale(PerformanceEvent $performanceEvent)
86
    {
87
        $seats = $this->getEntityManager()->getRepository(Seat::class)->getByVenue($performanceEvent->getVenue());
88
        foreach ($seats as $seat) {
89
            $tickets[] = self::changeStatusInTicket(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tickets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tickets = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90
                $performanceEvent,
91
                $seat,
92
                Ticket::STATUS_FREE,
93
                Ticket::STATUS_OFFLINE
94
            );
95
        }
96
97
        $tickets = [];
98
99
        foreach ($performanceEvent->getRowsForSale() as $forSale) {
100
            $seats = $this
101
                ->getEntityManager()
102
                ->getRepository(Seat::class)
103
                ->getByVenueSectorAndRow(
104
                    $forSale->getVenueSector(),
105
                    $forSale->getRow()
106
                );
107
            foreach ($seats as $seat) {
108
                $tickets[] = self::changeStatusInTicket(
109
                    $performanceEvent,
110
                    $seat,
111
                    Ticket::STATUS_OFFLINE,
112
                    Ticket::STATUS_FREE
113
                );
114
            }
115
        }
116
117
        $count = count($tickets);
118
        return $count;
119
    }
120
121
    /**
122
     * @param PerformanceEvent $performanceEvent
123
     * @param Seat $seat
124
     * @param string $oldStatus
125
     * @param string $newStatus
126
     * @return object
127
     */
128
    public function changeStatusInTicket(PerformanceEvent $performanceEvent, Seat $seat, $oldStatus, $newStatus)
129
    {
130
        $ticket = $this->findOneBy([
131
            'performanceEvent' => $performanceEvent,
132
            'seat' => $seat,
133
            'status' => $oldStatus,
134
        ]);
135
        if ($ticket) {
136
            $ticket->setStatus($newStatus);
137
            $this->getEntityManager()->persist($ticket);
138
139
            $this->getEntityManager()->flush();
140
        }
141
        return $ticket;
142
    }
143
}
144