Completed
Push — checkout-consistent-prices ( af49ca )
by Kamil
13:24
created

GenerateCouponsCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\PromotionBundle\Command;
15
16
use Sylius\Component\Core\Model\PromotionInterface;
17
use Sylius\Component\Core\Repository\PromotionRepositoryInterface;
18
use Sylius\Component\Promotion\Generator\PromotionCouponGeneratorInstruction;
19
use Sylius\Component\Promotion\Generator\PromotionCouponGeneratorInstructionInterface;
20
use Sylius\Component\Promotion\Generator\PromotionCouponGeneratorInterface;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
27
final class GenerateCouponsCommand extends Command
28
{
29
    /** @var PromotionRepositoryInterface */
30
    private $promotionRepository;
31
32
    /** @var PromotionCouponGeneratorInterface */
33
    private $couponGenerator;
34
35
    public function __construct(
36
        PromotionRepositoryInterface $promotionRepository,
37
        PromotionCouponGeneratorInterface $couponGenerator
38
    ) {
39
        parent::__construct();
40
41
        $this->promotionRepository = $promotionRepository;
42
        $this->couponGenerator = $couponGenerator;
43
    }
44
45
    protected function configure(): void
46
    {
47
        $this
48
            ->setName('sylius:promotion:generate-coupons')
49
            ->setDescription('Generates coupons for a given promotion')
50
            ->addArgument('promotion-code', InputArgument::REQUIRED, 'Code of the promotion')
51
            ->addArgument('count', InputArgument::REQUIRED, 'Amount of coupons to generate')
52
            ->addOption('length', 'len', InputOption::VALUE_OPTIONAL, 'Length of the coupon code (default 10)', 10)
53
        ;
54
    }
55
56
    public function execute(InputInterface $input, OutputInterface $output): int
57
    {
58
        /** @var string $promotionCode */
59
        $promotionCode = $input->getArgument('promotion-code');
60
61
        /** @var PromotionInterface|null $promotion */
62
        $promotion = $this->promotionRepository->findOneBy(['code' => $promotionCode]);
63
64
        if ($promotion === null) {
65
            $output->writeln('<error>No promotion found with this code</error>');
66
67
            return 1;
68
        }
69
70
        if (!$promotion->isCouponBased()) {
71
            $output->writeln('<error>This promotion is not coupon based</error>');
72
73
            return 1;
74
        }
75
76
        $instruction = $this->getGeneratorInstructions(
77
            (int) $input->getArgument('count'),
78
            (int) $input->getOption('length')
79
        );
80
81
        try {
82
            $this->couponGenerator->generate($promotion, $instruction);
83
        } catch (\Exception $exception) {
84
            $output->writeln('<error>' . $exception->getMessage() . '</error>');
85
86
            return 1;
87
        }
88
89
        $output->writeln('<info>Coupons have been generated</info>');
90
91
        return 0;
92
    }
93
94
    public function getGeneratorInstructions(int $count, int $codeLength): PromotionCouponGeneratorInstructionInterface
95
    {
96
        $instruction = new PromotionCouponGeneratorInstruction();
97
        $instruction->setAmount($count);
98
        $instruction->setCodeLength($codeLength);
99
100
        return $instruction;
101
    }
102
}
103