Completed
Pull Request — master (#151)
by Ihor
12:12
created

TicketRepository::batchRemove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Repository;
4
5
use AppBundle\Entity\PerformanceEvent;
6
use AppBundle\Entity\Ticket;
7
use AppBundle\Exception\Ticket\NotRemovableSetException;
8
9
class TicketRepository extends AbstractRepository
10
{
11
12
    /**
13
     * @param PerformanceEvent $performanceEvent
14
     *
15
     * @return Ticket[]
16
     * @throws NotRemovableSetException
17
     */
18
    public function getRemovableTicketSet(PerformanceEvent $performanceEvent): array
19
    {
20
        $ticket = $this->findOneBy([
21
            'performanceEvent' => $performanceEvent,
22
            'status' => [Ticket::STATUS_BOOKED, Ticket::STATUS_PAID]
23
        ]);
24
25
        if (!empty($ticket)) {
26
            throw new NotRemovableSetException(
27
                sprintf(
28
                    'Impossible to remove tickets for PerformanceEvent: %s.',
29
                    $performanceEvent
30
                )
31
            );
32
        }
33
34
        $tickets = $this->findBy([
35
            'performanceEvent' => $performanceEvent
36
        ]);
37
38
        return $tickets;
39
    }
40
41
    /**
42
     * @param PerformanceEvent $performanceEvent
43
     *
44
     * @return bool
45
     */
46
    public function isGeneratedSet(PerformanceEvent $performanceEvent)
47
    {
48
        $ticket = $this->findOneBy([
49
            'performanceEvent' => $performanceEvent
50
        ]);
51
        return !empty($ticket);
52
    }
53
54
    /**
55
     * @param Ticket[] $tickets
56
     */
57
    public function batchSave(array $tickets)
58
    {
59
        foreach ($tickets as $ticket) {
60
            $this->save($ticket, false);
61
        }
62
        $this->getEntityManager()->flush();
63
    }
64
65
    /**
66
     * @param Ticket[] $tickets
67
     */
68
    public function batchRemove(array $tickets)
69
    {
70
        foreach ($tickets as $ticket) {
71
            $this->remove($ticket, false);
72
        }
73
        $this->getEntityManager()->flush();
74
    }
75
}
76