1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Command; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\PerformanceEvent; |
6
|
|
|
use AppBundle\Repository\PerformanceEventRepository; |
7
|
|
|
use AppBundle\Repository\TicketRepository; |
8
|
|
|
use AppBundle\Services\Ticket\GenerateSetHandler; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class GenerateTicketsCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** @var GenerateSetHandler */ |
19
|
|
|
private $ticketGenerateSet; |
20
|
|
|
|
21
|
|
|
/** @var PerformanceEventRepository */ |
22
|
|
|
private $performanceEventRepository; |
23
|
2 |
|
|
24
|
|
|
/** @var TicketRepository */ |
25
|
|
|
private $ticketRepository; |
26
|
|
|
|
27
|
2 |
|
public function __construct( |
28
|
2 |
|
GenerateSetHandler $ticketGenerateSet, |
29
|
2 |
|
PerformanceEventRepository $performanceEventRepository, |
30
|
2 |
|
TicketRepository $ticketRepository |
31
|
|
|
) { |
32
|
2 |
|
parent::__construct(); |
33
|
|
|
$this->ticketGenerateSet = $ticketGenerateSet; |
34
|
|
|
$this->performanceEventRepository = $performanceEventRepository; |
35
|
2 |
|
$this->ticketRepository = $ticketRepository; |
36
|
2 |
|
} |
37
|
2 |
|
|
38
|
2 |
|
protected function configure() |
39
|
2 |
|
{ |
40
|
2 |
|
$this |
41
|
|
|
->setName('app:generate-tickets') |
42
|
2 |
|
->setDescription('Generate new Set of Tickets for performanceEvent') |
43
|
2 |
|
->addArgument( |
44
|
2 |
|
'performanceEventId', |
45
|
2 |
|
InputArgument::REQUIRED, |
46
|
2 |
|
'The Performance Event ID.' |
47
|
2 |
|
) |
48
|
|
|
->addOption( |
49
|
|
|
'force', |
50
|
2 |
|
'f', |
51
|
|
|
InputOption::VALUE_OPTIONAL, |
52
|
|
|
'Remove previously generated tickets set for PerformanceEvent and generates new one.', |
53
|
|
|
false |
54
|
|
|
) |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$startTime = new \DateTime('now'); |
62
|
|
|
$output->writeln('<comment>Running Tickets Generation</comment>'); |
63
|
|
|
$performanceEventId = (int) $input->getArgument('performanceEventId') ?: null; |
64
|
|
|
$force = (bool) $input->getOption('force') ? true : false; |
65
|
|
|
$performanceEvent = $this->performanceEventRepository->getById($performanceEventId); |
66
|
|
|
|
67
|
|
|
if ($force) { |
|
|
|
|
68
|
|
|
//TODO remove existed tickets for PerformanceEvent if they exists |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($this->ticketsExists($performanceEvent)) { |
72
|
|
|
throw new \Exception('Tickets already exist for: '. $performanceEvent); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$tickets = $this->ticketGenerateSet->handle($performanceEvent); |
76
|
|
|
$this->ticketRepository->batchSave($tickets); |
|
|
|
|
77
|
|
|
$output->writeln(sprintf('<info>SUCCESS. %s tickets were generated</info>', count($tickets))); |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
$output->writeln('<error>ERROR Generating Tickets: '.$e->getMessage().'</error>'); |
80
|
|
|
} finally { |
81
|
|
|
$finishTime = new \DateTime('now'); |
82
|
|
|
$interval = $startTime->diff($finishTime); |
83
|
|
|
$output->writeln(sprintf('<comment>DONE in %s seconds<comment>', $interval->s)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param PerformanceEvent $performanceEvent |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
protected function ticketsExists(PerformanceEvent $performanceEvent) |
93
|
|
|
{ |
94
|
|
|
return (bool) count($this->ticketRepository->findBy([ |
95
|
|
|
'performanceEvent' => $performanceEvent |
96
|
|
|
])); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.