Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

CreateCommand::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 36

Duplication

Lines 13
Ratio 36.11 %

Importance

Changes 0
Metric Value
dl 13
loc 36
rs 9.0328
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Admin\Token;
4
5
use Exception;
6
use Magento\Integration\Model\Oauth\Token;
7
use Magento\Integration\Model\Oauth\TokenFactory;
8
use Magento\User\Model\User;
9
use N98\Magento\Command\AbstractMagentoCommand;
10
use Symfony\Component\Console\Helper\QuestionHelper;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Question\Question;
16
17
/**
18
 * Class CreateCommand
19
 * @package N98\Magento\Command\Admin\Token
20
 */
21
class CreateCommand extends AbstractMagentoCommand
22
{
23
    /**
24
     * @var TokenFactory
25
     */
26
    private $tokenModelFactory;
27
28
    /**
29
     * @var User
30
     */
31
    protected $userModel;
32
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('admin:token:create')
37
            ->addArgument('username', InputArgument::OPTIONAL, 'Username')
38
            ->addOption('no-newline', null, InputOption::VALUE_NONE, 'do not output the trailing newline')
39
            ->setDescription('Create a new token for an admin user.');
40
    }
41
42
    /**
43
     * @param Token $tokenModelFactory
0 ignored issues
show
Documentation introduced by
Should the type for parameter $tokenModelFactory not be TokenFactory?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
44
     * @param User $userModel $userModel
45
     */
46
    public function inject(
47
        TokenFactory $tokenModelFactory,
48
        User $userModel
49
    ) {
50
        $this->tokenModelFactory = $tokenModelFactory;
51
        $this->userModel = $userModel;
52
    }
53
54
    /**
55
     * @param InputInterface $input
56
     * @param OutputInterface $output
57
     *
58
     * @return int|void
59
     * @throws Exception
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        // Detect and Init Magento
64
        $this->detectMagento($output, true);
65
        if (!$this->initMagento()) {
66
            return;
67
        }
68
69
        // Username
70
        $username = $input->getArgument('username');
71 View Code Duplication
        if ($username === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            /** @var $questionHelper QuestionHelper */
73
            $questionHelper = $this->getHelper('question');
74
            $question = new Question('<question>Username:</question>');
75
            $question->setValidator(function ($value) {
76
                if ($value === '') {
77
                    throw new \Exception('Please enter a valid username');
78
                }
79
80
                return $value;
81
            });
82
            $username = $questionHelper->ask($input, $output, $question);
83
        }
84
85
        $adminUser = $this->userModel->loadByUsername($username);
86
        if ($adminUser->getId() <= 0) {
87
            $output->writeln('<error>User was not found</error>');
88
            return;
89
        }
90
91
        /** @var Token $tokenModel */
92
        $tokenModel = $this->tokenModelFactory->create();
93
        $tokenModel->createAdminToken($adminUser->getId());
94
95
        $output->write($tokenModel->getToken(), !$input->getOption('no-newline'));
96
    }
97
}
98