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

GenerateTicketsCommand::ticketsExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
ccs 0
cts 0
cp 0
crap 2
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) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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 the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$tickets is of type array<integer,object<AppBundle\Entity\Ticket>>, but the function expects a array<integer,object<App...\Domain\Ticket\Ticket>>.

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...
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