Completed
Push — develop ( 058674...85e12e )
by Patrick
03:47
created

Command/CreateAccessTokenCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Mailxpert\APIBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\Question;
11
12
class CreateAccessTokenCommand extends ContainerAwareCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('mailxpert:api:access_token:create')
18
            ->setDescription('Create a persistent mailXpert Access Token in the database')
19
            ->setDefinition([
20
                new InputOption('accessToken', '', InputOption::VALUE_REQUIRED, 'Access token'),
21
                new InputOption('refreshToken', '', InputOption::VALUE_REQUIRED, 'Refresh token'),
22
                new InputOption('expireAt', '', InputOption::VALUE_REQUIRED, 'Expire at'),
23
                new InputOption('refreshTokenExpireAt', '', InputOption::VALUE_REQUIRED, 'Expire at'),
24
                new InputOption('scope', '', InputOption::VALUE_OPTIONAL, 'scope'),
25
            ])
26
        ;
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $accessToken = $this->getContainer()->get('mailxpert_api.access_token_manager')->createAccessToken();
32
33
        $accessToken->setAccessToken($input->getOption('accessToken'));
34
        $accessToken->setRefreshToken($input->getOption('refreshToken'));
35
        $accessToken->setExpireAt($input->getOption('expireAt'));
36
        $accessToken->setRefreshTokenExpireAt($input->getOption('refreshTokenExpireAt'));
37
        $accessToken->setScope($input->getOption('scope'));
38
39
        $this->getContainer()->get('doctrine')->getManager()->persist($accessToken);
40
        $this->getContainer()->get('doctrine')->getManager()->flush();
41
42
        $output->writeln(sprintf('<info>Access token successfully created with ID %s</info>', $accessToken->getId()));
43
    }
44
45
    protected function interact(InputInterface $input, OutputInterface $output)
46
    {
47
        $output->writeln('Save a mailXpert Access Token (obtain a valid Access Token via the Developer console at https://dev.mailxpert.ch)');
48
49
        $this->askQuestion($input, $output, 'accessToken', 'access token');
50
        $this->askQuestion($input, $output, 'refreshToken', 'refresh token');
51
        $this->askQuestion($input, $output, 'expireAt', 'expire at value');
52
        $this->askQuestion($input, $output, 'refreshTokenExpireAt', 'refresh token expire at value');
53
//        $this->askQuestion($input, $output, 'scope', 'scope');
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
    }
55
56
    private function askQuestion(InputInterface $input, OutputInterface $output, $option, $optionName)
57
    {
58
        $questionHelper = $this->getQuestionHelper();
59
60
        $value = null;
61
62
        try {
63
            $value = $input->getOption($option) ? $input->getOption($option) : null;
64
        } catch (\Exception $error) {
65
            $output->writeln($error->getMessage());
66
        }
67
68
        if (null === $value) {
69
            while ($value === null) {
70
                $question = new Question(sprintf("Enter the %s: ", $optionName));
71
                $value = $questionHelper->ask($input, $output, $question);
72
            }
73
            $input->setOption($option, $value);
74
        }
75
    }
76
77
    protected function getQuestionHelper()
78
    {
79
        $question = $this->getHelperSet()->get('question');
80
        if (!$question) {
81
            $this->getHelperSet()->set($question = new QuestionHelper());
82
        }
83
84
        return $question;
85
    }
86
}