1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SyliusCart\CartBundle\Command; |
4
|
|
|
|
5
|
|
|
use Broadway\CommandHandling\CommandBusInterface; |
6
|
|
|
use Broadway\UuidGenerator\UuidGeneratorInterface; |
7
|
|
|
use Ramsey\Uuid\Uuid; |
8
|
|
|
use SyliusCart\Domain\Command\InitializeCart; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
14
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
15
|
|
|
use Symfony\Component\Console\Question\Question; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class InitializeCartCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var CommandBusInterface |
24
|
|
|
*/ |
25
|
|
|
private $commandBus; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var UuidGeneratorInterface |
29
|
|
|
*/ |
30
|
|
|
private $uuidGenerator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param CommandBusInterface $commandBus |
34
|
|
|
* @param UuidGeneratorInterface $uuidGenerator |
35
|
|
|
*/ |
36
|
|
|
public function __construct(CommandBusInterface $commandBus, UuidGeneratorInterface $uuidGenerator) |
37
|
|
|
{ |
38
|
|
|
$this->commandBus = $commandBus; |
39
|
|
|
$this->uuidGenerator = $uuidGenerator; |
40
|
|
|
|
41
|
|
|
parent::__construct(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function configure() |
48
|
|
|
{ |
49
|
|
|
$this |
50
|
|
|
->setName('sylius:cart:initialize') |
51
|
|
|
->setDescription('Initialize your event sourced cart') |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
/** @var QuestionHelper $helper */ |
61
|
|
|
$helper = $this->getHelper('question'); |
62
|
|
|
|
63
|
|
|
$question = new ChoiceQuestion( |
64
|
|
|
'Do you want init new cart or mocked one?', |
65
|
|
|
['new', 'mocked'], |
66
|
|
|
0 |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$answer = $helper->ask($input, $output, $question); |
70
|
|
|
|
71
|
|
|
if ('mocked' === $answer) { |
72
|
|
|
$cartId = $this->uuidGenerator->generate(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ('new' === $answer) { |
76
|
|
|
$cartId = Uuid::uuid4()->toString(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$currencyCode = $helper->ask($input, $output, new Question('Cart currency code: ', 'USD')); |
80
|
|
|
|
81
|
|
|
$initializeCart = InitializeCart::create(Uuid::fromString($cartId), $currencyCode); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->commandBus->dispatch($initializeCart); |
84
|
|
|
|
85
|
|
|
$output->writeln(sprintf('Your cart id: "%s"', $cartId)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: