Completed
Push — master ( 6764cb...4e8215 )
by Christian
03:24 queued 11s
created

CreateCommand::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 4
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\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Class CreateCommand
17
 * @package N98\Magento\Command\Admin\Token
18
 */
19
class CreateCommand extends AbstractMagentoCommand
20
{
21
    /**
22
     * @var TokenFactory
23
     */
24
    private $tokenModelFactory;
25
26
    /**
27
     * @var User
28
     */
29
    protected $userModel;
30
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('admin:token:create')
35
            ->addArgument('username', InputArgument::OPTIONAL, 'Username')
36
            ->addOption('no-newline', null, InputOption::VALUE_NONE, 'do not output the trailing newline')
37
            ->setDescription('Create a new token for an admin user.');
38
    }
39
40
    /**
41
     * @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...
42
     * @param User $userModel $userModel
43
     */
44
    public function inject(
45
        TokenFactory $tokenModelFactory,
46
        User $userModel
47
    ) {
48
        $this->tokenModelFactory = $tokenModelFactory;
49
        $this->userModel = $userModel;
50
    }
51
52
    /**
53
     * @param InputInterface $input
54
     * @param OutputInterface $output
55
     *
56
     * @return int|void
57
     * @throws Exception
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        // Detect and Init Magento
62
        $this->detectMagento($output, true);
63
        if (!$this->initMagento()) {
64
            return;
65
        }
66
67
        /** @var $dialog DialogHelper */
68
        $dialog = $this->getHelper('dialog');
69
70
        // Username
71
        if (($username = $input->getArgument('username')) == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $username = $input->getArgument('username') of type string|string[]|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
72
            $username = $dialog->ask($output, '<question>Username:</question>');
73
        }
74
75
        $adminUser = $this->userModel->loadByUsername($username);
76
        if ($adminUser->getId() <= 0) {
77
            $output->writeln('<error>User was not found</error>');
78
            return;
79
        }
80
81
        /** @var Token $tokenModel */
82
        $tokenModel = $this->tokenModelFactory->create();
83
        $tokenModel->createAdminToken($adminUser->getId());
84
85
        $output->write($tokenModel->getToken(), !$input->getOption('no-newline'));
86
    }
87
}
88