Completed
Push — develop ( 73bd0a...ccb498 )
by Tom
05:04
created

CreateCommand::execute()   C

Complexity

Conditions 9
Paths 49

Size

Total Lines 74
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 74
rs 5.9292
cc 9
eloc 43
nc 49
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Magento\Command\Customer;
4
5
use Magento\Customer\Api\AccountManagementInterface;
6
use Magento\Framework\App\State;
7
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class CreateCommand extends AbstractCustomerCommand
14
{
15
    /**
16
     * @var AccountManagementInterface
17
     */
18
    private $accountManagement;
19
20
    /**
21
     * @var State
22
     */
23
    private $appState;
24
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('customer:create')
29
            ->addArgument('email', InputArgument::OPTIONAL, 'Email')
30
            ->addArgument('password', InputArgument::OPTIONAL, 'Password')
31
            ->addArgument('firstname', InputArgument::OPTIONAL, 'Firstname')
32
            ->addArgument('lastname', InputArgument::OPTIONAL, 'Lastname')
33
            ->addArgument('website', InputArgument::OPTIONAL, 'Website')
34
            ->addOption(
35
                'format',
36
                null,
37
                InputOption::VALUE_OPTIONAL,
38
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
39
            )
40
            ->setDescription('Creates a new customer/user for shop frontend.');
41
    }
42
43
    /**
44
     * @param AccountManagementInterface $accountManagement
45
     */
46
    public function inject(
47
        AccountManagementInterface $accountManagement,
48
        State $appState
49
    ) {
50
        $this->accountManagement = $accountManagement;
51
        $this->appState = $appState;
52
    }
53
54
    /**
55
     * @param InputInterface $input
56
     * @param OutputInterface $output
57
     * @return int|null|void
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $this->detectMagento($output, true);
62
        if ($this->initMagento()) {
63
            $dialog = $this->getHelperSet()->get('dialog');
64
65
            // Email
66
            $email = $this->getHelperSet()->get('parameter')->askEmail($input, $output);
67
68
            // Password
69
            $password = $this->getHelperSet()->get('parameter')->askPassword($input, $output, 'password', false);
70
71
            // Firstname
72
            if (($firstname = $input->getArgument('firstname')) == null) {
73
                $firstname = $dialog->ask($output, '<question>Firstname:</question>');
74
            }
75
76
            // Lastname
77
            if (($lastname = $input->getArgument('lastname')) == null) {
78
                $lastname = $dialog->ask($output, '<question>Lastname:</question>');
79
            }
80
81
            $website = $this->getHelperSet()->get('parameter')->askWebsite($input, $output);
82
83
            // create new customer
84
            $customer = $this->getCustomer();
85
            $customer->setWebsiteId($website->getId());
86
            $customer->loadByEmail($email);
87
88
            $outputPlain = $input->getOption('format') === null;
89
90
            $table = array();
91
            if (!$customer->getId()) {
92
                $customer->setWebsiteId($website->getId());
93
                $customer->setEmail($email);
94
                $customer->setFirstname($firstname);
95
                $customer->setLastname($lastname);
96
97
                try {
98
                    $this->appState->emulateAreaCode('frontend', function () use ($customer, $password) {
99
                        $this->accountManagement->createAccount(
100
                            $customer->getDataModel(),
101
                            $password
102
                        );
103
                    });
104
                } catch (\Exception $e) {
105
                    $output->writeln('<error>' . $e->getMessage() . '</error>');
106
                }
107
108
                if ($outputPlain) {
109
                    $output->writeln(
110
                        sprintf(
111
                            '<info>Customer <comment>%s</comment> successfully created</info>',
112
                            $email
113
                        )
114
                    );
115
                } else {
116
                    $table[] = array(
117
                        $email, $password, $firstname, $lastname,
118
                    );
119
                }
120
            } else {
121
                if ($outputPlain) {
122
                    $output->writeln('<error>Customer ' . $email . ' already exists</error>');
123
                }
124
            }
125
126
            if (!$outputPlain) {
127
                $this->getHelper('table')
128
                    ->setHeaders(array('email', 'password', 'firstname', 'lastname'))
129
                    ->renderByFormat($output, $table, $input->getOption('format'));
130
            }
131
        }
132
    }
133
}
134