1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Command; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\PerformanceEvent; |
6
|
|
|
use AppBundle\Entity\Ticket; |
7
|
|
|
use AppBundle\Exception\Ticket\DuplicateSetException; |
8
|
|
|
use AppBundle\Repository\PerformanceEventRepository; |
9
|
|
|
use AppBundle\Repository\TicketRepository; |
10
|
|
|
use AppBundle\Services\Ticket\GenerateSetHandler; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
17
|
|
|
|
18
|
|
|
class GenerateTicketsCommand extends ContainerAwareCommand |
19
|
|
|
{ |
20
|
|
|
/** @var GenerateSetHandler */ |
21
|
|
|
private $ticketGenerateSet; |
22
|
|
|
|
23
|
|
|
/** @var PerformanceEventRepository */ |
24
|
|
|
private $performanceEventRepository; |
25
|
|
|
|
26
|
|
|
/** @var TicketRepository */ |
27
|
|
|
private $ticketRepository; |
28
|
|
|
|
29
|
2 |
|
public function __construct( |
30
|
|
|
GenerateSetHandler $ticketGenerateSet, |
31
|
|
|
PerformanceEventRepository $performanceEventRepository, |
32
|
|
|
TicketRepository $ticketRepository |
33
|
|
|
) { |
34
|
2 |
|
parent::__construct(); |
35
|
2 |
|
$this->ticketGenerateSet = $ticketGenerateSet; |
36
|
2 |
|
$this->performanceEventRepository = $performanceEventRepository; |
37
|
2 |
|
$this->ticketRepository = $ticketRepository; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
2 |
|
protected function configure() |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
2 |
|
->setName('app:generate-tickets') |
44
|
2 |
|
->setDescription('Generate new Set of Tickets for performanceEvent') |
45
|
2 |
|
->addArgument( |
46
|
2 |
|
'performanceEventId', |
47
|
2 |
|
InputArgument::REQUIRED, |
48
|
2 |
|
'The Performance Event ID.' |
49
|
|
|
) |
50
|
2 |
|
->addOption( |
51
|
2 |
|
'force', |
52
|
2 |
|
'f', |
53
|
2 |
|
InputOption::VALUE_OPTIONAL, |
54
|
2 |
|
'Remove previously generated tickets set for PerformanceEvent and generates new one.', |
55
|
2 |
|
false |
56
|
|
|
) |
57
|
|
|
; |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$startTime = new \DateTime('now'); |
64
|
|
|
|
65
|
|
|
$token = new AnonymousToken('console_user', 'console_user', ['ROLE_SUPER_ADMIN']); |
|
|
|
|
66
|
|
|
$this->getContainer()->get('security.token_storage')->setToken($token); |
67
|
|
|
|
68
|
|
|
$output->writeln('<comment>Running Tickets Generation</comment>'); |
69
|
|
|
$performanceEventId = (int) $input->getArgument('performanceEventId') ?: null; |
70
|
|
|
$force = (bool) $input->getOption('force') ? true : false; |
71
|
|
|
|
72
|
|
|
/** @var PerformanceEvent $performanceEvent */ |
73
|
|
|
$performanceEvent = $this->performanceEventRepository->getById($performanceEventId); |
74
|
|
|
|
75
|
|
|
if ($force) { |
76
|
|
|
$tickets = $this->ticketRepository->getRemovableTicketSet($performanceEvent); |
77
|
|
|
$this->ticketRepository->batchRemove($tickets); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->ticketRepository->isGeneratedSet($performanceEvent)) { |
81
|
|
|
throw new DuplicateSetException('Ticket Set already generated for: '. $performanceEvent); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @var Ticket[] $tickets */ |
85
|
|
|
$tickets = $this->ticketGenerateSet->handle($performanceEvent); |
86
|
|
|
$this->ticketRepository->batchSave($tickets); |
87
|
|
|
$output->writeln(sprintf('<info>SUCCESS. %s tickets were generated</info>', count($tickets))); |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
$output->writeln('<error>ERROR Generating Tickets: '.$e->getMessage().'</error>'); |
90
|
|
|
} finally { |
91
|
|
|
$finishTime = new \DateTime('now'); |
92
|
|
|
$interval = $startTime->diff($finishTime); |
93
|
|
|
$output->writeln(sprintf('<comment>DONE in %s seconds<comment>', $interval->s)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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: