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

AddProductToCartCommand::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 2
1
<?php
2
3
namespace SyliusCart\CartBundle\Command;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\UuidGenerator\UuidGeneratorInterface;
7
use SyliusCart\Domain\Command\AddProductToCart;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\QuestionHelper;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\Question;
13
14
/**
15
 * @author Arkadiusz Krakowiak <[email protected]>
16
 */
17
final class AddProductToCartCommand extends Command
18
{
19
    /**
20
     * @var CommandBusInterface
21
     */
22
    private $commandBus;
23
24
    /**
25
     * @var UuidGeneratorInterface
26
     */
27
    private $uuidGenerator;
28
29
    /**
30
     * @param CommandBusInterface $commandBus
31
     * @param UuidGeneratorInterface $uuidGenerator
32
     */
33
    public function __construct(CommandBusInterface $commandBus, UuidGeneratorInterface $uuidGenerator)
34
    {
35
        $this->commandBus = $commandBus;
36
        $this->uuidGenerator = $uuidGenerator;
37
38
        parent::__construct();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function configure()
45
    {
46
        $this
47
            ->setName('sylius:cart:add-product')
48
            ->setDescription('Add product to cart')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        /** @var QuestionHelper $helper */
58
        $helper = $this->getHelper('question');
59
60
        $cartId = $helper->ask(
61
            $input,
62
            $output,
63
            new Question(
64
                sprintf('Cart id (%s): ', $this->uuidGenerator->generate()),
65
                $this->uuidGenerator->generate()
66
            )
67
        );
68
69
        $productCode = $helper->ask($input, $output, new Question('Product code: ', 'Mug'));
70
        $quantity = $helper->ask($input, $output, new Question('Quantity: ', 1));
71
        $price = $helper->ask($input, $output, new Question('How much does it costs in cents: ', 1000));
72
        $currency = $helper->ask($input, $output, new Question('In which currency: ', 'EUR'));
73
74
        $addCartItem = AddProductToCart::create($cartId, $productCode, $quantity, $price, $currency);
75
76
        $this->commandBus->dispatch($addCartItem);
77
78
        $output->writeln(sprintf('Product added!'));
79
    }
80
}
81