Passed
Push — master ( d16246...1e2634 )
by Derek Stephen
02:49
created

ClientCommand::execute()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 48
nc 17
nop 2
dl 0
loc 73
ccs 0
cts 49
cp 0
crap 42
rs 8.5123
c 0
b 0
f 0

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 OAuth\Command;
4
5
use Del\Service\UserService;
6
use OAuth\Client;
7
use OAuth\Repository\ScopeRepository;
8
use OAuth\Scope;
9
use OAuth\Service\ClientService;
10
use OAuth\OAuthUser as User;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ChoiceQuestion;
15
use Symfony\Component\Console\Question\ConfirmationQuestion;
16
use Symfony\Component\Console\Question\Question;
17
18
/**
19
 * Class ClientCommand
20
 * @package OAuth\Command
21
 */
22
class ClientCommand extends Command
23
{
24
    /**
25
     * @var ClientService $clientService
26
     */
27
    private $clientService;
28
29
    /**
30
     * @var UserService $userService
31
     */
32
    private $userService;
33
34
    /**
35
     * @var ScopeRepository $scopeRepository
36
     */
37
    private $scopeRepository;
38
39
    public function __construct(ClientService $clientService, UserService $userService, ScopeRepository $scopeRepository)
40
    {
41
        $this->clientService = $clientService;
42
        $this->userService = $userService;
43
        $this->scopeRepository = $scopeRepository;
44
        parent::__construct('client:create');
45
    }
46
47
    /**
48
     * configure options
49
     */
50
    protected function configure()
51
    {
52
        $this->setDescription('Creates a new client.');
53
        $this->setHelp('Create a new OAuth2 client application');
54
    }
55
56
    /**
57
     * @param InputInterface $input
58
     * @param OutputInterface $output
59
     * @throws \Doctrine\ORM\OptimisticLockException
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $output->writeln('Bone API client creator');
64
        $helper = $this->getHelper('question');
65
66
        $question = new Question('Enter the email of the account: ', false);
67
        $email = $helper->ask($input, $output, $question);
68
69
        $this->userService->setUserClass(User::class);
70
        /** @var User $user */
71
        $user = $this->userService->findUserByEmail($email);
72
73
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type OAuth\OAuthUser, thus it always evaluated to true.
Loading history...
74
            $output->writeln('User not found. Exiting.');
75
            return null;
76
        }
77
78
        $question = new Question('Give a name for this application: ', false);
79
        $name = $helper->ask($input, $output, $question);
80
81
        $question = new Question('Give a description: ', false);
82
        $description = $helper->ask($input, $output, $question);
83
84
        $question = new Question('Give an icon URL: ', false);
85
        $icon = $helper->ask($input, $output, $question);
86
87
        $question = new ChoiceQuestion('Select the Authorisation Grant type: ',[
88
            'auth_code', 'implicit', 'password', 'client_credentials'
89
        ]);
90
        $authGrant = $helper->ask($input, $output, $question);
91
92
        $question = new Question('Give a redirect URI: ', '');
93
        $uri = $helper->ask($input, $output, $question);
94
95
        $confidential = true;
96
        if ($authGrant !== 'client_credentials') {
97
            $question = new ConfirmationQuestion('Is this a phone app or JS client ? ', false);
98
            $public = $helper->ask($input, $output, $question);
99
            $confidential = !$public;
100
        }
101
102
        $scopes = $this->scopeRepository->findAll();
103
        $choices = [];
104
        /** @var Scope $scope */
105
        foreach($scopes as $index => $scope) {
106
            $choices[$index] = $scope->getIdentifier();
107
        }
108
109
        $question = new ChoiceQuestion('Which scopes would you like to add?', $choices);
110
        $question->setMultiselect(true);
111
        $scopeChoices = $helper->ask($input, $output, $question);
112
113
        $client = new Client();
114
        $client->setName($name);
115
        $client->setDescription($description);
116
        $client->setIcon($icon);
117
        $client->setGrantType($authGrant);
118
        $client->setIdentifier(md5($name));
119
        $client->setRedirectUri($uri);
120
        $client->setConfidential($confidential);
121
        $client->setUser($user);
122
        foreach ($scopeChoices as $scopeIndex => $name) {
123
            $scope = $scopes[$scopeIndex];
124
            $client->getScopes()->add($scope);
125
        }
126
127
        if ($confidential) {
128
            $this->clientService->generateSecret($client);
129
        }
130
131
        $this->clientService->getClientRepository()->create($client);
132
133
        $output->writeln('Client created.');
134
    }
135
}