Completed
Pull Request — develop (#165)
by Robbie
04:15
created

CreateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\GiftCard;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputArgument;
9
10
class CreateCommand extends AbstractGiftCardCommand
11
{
12
    /**
13
     * Setup
14
     * 
15
     * @return void
16
     */
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('giftcard:create')
21
            ->addArgument('amount', InputArgument::REQUIRED, 'Amount for new gift card')
22
            ->addOption('website', null, InputOption::VALUE_OPTIONAL, 'Website ID to attach gift card to')
23
            ->addOption('expires', null, InputOption::VALUE_OPTIONAL, 'Expiration date in YYYY-MM-DD format')
24
            ->setDescription('Create a new gift card with a specified amount');
25
26
        $help = <<<HELP
27
Create a new gift card with a specified amount
28
HELP;
29
        $this->setHelp($help);
30
    }
31
32
    /**
33
     * @param \Symfony\Component\Console\Input\InputInterface $input
34
     * @param \Symfony\Component\Console\Output\OutputInterface $output
35
     * @return void
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->detectMagento($output, true);
40
        if (!$this->initMagento()) {
41
            return;
42
        }
43
44
        $this->setAdminArea();
45
46
        $giftcard = $this->getGiftcard();
47
        $giftcard->setData(
48
            array(
49
                'status'        => 1,
50
                'is_redeemable' => 1,
51
                'website_id'    => $input->getOption('website')
52
                    ? : $this->getObjectManager()->get('Magento\Store\Model\StoreManager')->getWebsite(true)->getId(),
53
                'balance'       => $input->getArgument('amount'),
54
                'date_expires'  => $input->getOption('expires')
55
            )
56
        );
57
        
58
        $giftcard->save();
59
        if (!$giftcard->getId()) {
60
            $output->writeln('<error>Failed to create gift card</error>');
61
            return;
62
        }
63
64
        $output->writeln('<info>Gift card <comment>' . $giftcard->getCode() . '</comment> was created</info>');
65
    }
66
}
67