Completed
Push — master ( d94ed5...244927 )
by Arkadiusz
14:18 queued 12:13
created

InitializeCartCommand::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 2
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);
0 ignored issues
show
Bug introduced by
The variable $cartId does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
82
83
        $this->commandBus->dispatch($initializeCart);
84
85
        $output->writeln(sprintf('Your cart id: "%s"', $cartId));
86
    }
87
}
88