ClientScopeCommand::outputScopes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OAuth\Command;
4
5
use Doctrine\Common\Collections\Collection;
6
use Exception;
7
use OAuth\Client;
8
use OAuth\Repository\ScopeRepository;
9
use OAuth\Scope;
10
use OAuth\Service\ClientService;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Helper\QuestionHelper;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Question\ChoiceQuestion;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
use Symfony\Component\Console\Question\Question;
19
20
/**
21
 * Class ClientCommand
22
 * @package OAuth\Command
23
 */
24
class ClientScopeCommand extends Command
25
{
26
    /**
27
     * @var ClientService $clientService
28
     */
29
    private $clientService;
30
31
    /**
32
     * @var ScopeRepository $scopeRepository
33
     */
34
    private $scopeRepository;
35
36
    /** @var QuestionHelper $helper */
37
    private $helper;
0 ignored issues
show
introduced by
The private property $helper is not used, and could be removed.
Loading history...
38
39
    public function __construct(ClientService $clientService, ScopeRepository $scopeRepository)
40
    {
41
        $this->clientService = $clientService;
42
        $this->scopeRepository = $scopeRepository;
43
        parent::__construct('client:scope');
44
        $this->addArgument('operation', InputArgument::REQUIRED, 'list, add, or remove.');
45
        $this->addArgument('client', InputArgument::OPTIONAL, 'The client identifier.');
46
        $this->addArgument('scope', InputArgument::OPTIONAL, 'The scope name when adding or removing.');
47
    }
48
49
    /**
50
     * configure options
51
     */
52
    protected function configure()
53
    {
54
        $this->setDescription('Add, remove, or list scopes for each client.');
55
        $this->setHelp('Client scope administration');
56
    }
57
58
    /**
59
     * @param InputInterface $input
60
     * @param OutputInterface $output
61
     * @return int|void|null
62
     * @throws Exception
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $output->writeln(' ');
67
        $output->writeln('Client scope administration');
68
        $operation = $input->getArgument('operation');
69
        switch ($operation) {
70
            case 'list';
71
                $this->listScopes($input, $output);
72
                break;
73
            case 'add';
74
                $this->addScope($input, $output);
75
                break;
76
            case 'remove';
77
                $this->removeScope($input, $output);
78
                break;
79
        }
80
        $output->writeln(' ');
81
    }
82
83
    /**
84
     * @param InputInterface $input
85
     * @param string $argName
86
     * @return string|string[]|null
87
     * @throws Exception
88
     */
89
    private function getArgOrGetUpset(InputInterface $input, string $argName)
90
    {
91
        $value = $input->getArgument($argName);
92
        if (!$value) {
93
            throw new Exception('No ' . $argName . ' provided');
94
        }
95
96
        return $value;
97
    }
98
99
    /**
100
     * @param InputInterface $input
101
     * @param OutputInterface $output
102
     */
103
    private function listScopes(InputInterface $input, OutputInterface $output)
104
    {
105
        $clientId = $input->getArgument('client');
106
107
        $client = $this->fetchClient($output, $clientId);
0 ignored issues
show
Bug introduced by
It seems like $clientId can also be of type null and string[]; however, parameter $id of OAuth\Command\ClientScopeCommand::fetchClient() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $client = $this->fetchClient($output, /** @scrutinizer ignore-type */ $clientId);
Loading history...
108
        if (!$client instanceof Client) {
0 ignored issues
show
introduced by
$client is always a sub-type of OAuth\Client.
Loading history...
109
            $output->writeln('No client found');
110
            return;
111
        }
112
113
        $scopes = $client->getScopes();
114
        $this->outputScopes($output, $client, $scopes);
115
116
        return;
117
    }
118
119
    /**
120
     * @param OutputInterface $output
121
     * @param Collection $scopes
122
     */
123
    private function outputScopes(OutputInterface $output, Client $client, Collection $scopes)
124
    {
125
        $output->writeln('Listing scopes for ' . $client->getName() . '.');
126
127
        if ($scopes->count()) {
128
            /** @var Scope $scope */
129
            foreach ($scopes as $scope) {
130
                $output->writeln(' - ' . $scope->getIdentifier());
131
            }
132
        } else {
133
            $output->writeln('No scopes set for ' . $client->getName() . '.');
134
        }
135
    }
136
137
    /**
138
     * @param string $id
139
     * @return Client
140
     */
141
    private function fetchClient(OutputInterface $output, string $id)
142
    {
143
        $output->writeln('Fetching client ' . $id .'...');
144
        /** @var Client $client */
145
        $client = $this->clientService
146
                    ->getClientRepository()
147
                    ->getClientEntity( $id, null, null, false);
148
149
        return $client;
150
    }
151
152
    /**
153
     * @param InputInterface $input
154
     * @param OutputInterface $output
155
     * @throws Exception
156
     */
157
    private function addScope(InputInterface $input, OutputInterface $output)
158
    {
159
        $clientId = $input->getArgument('client');
160
        $scopeId = $this->getArgOrGetUpset($input, 'scope');
161
162
        $client = $this->fetchClient($output, $clientId);
0 ignored issues
show
Bug introduced by
It seems like $clientId can also be of type null and string[]; however, parameter $id of OAuth\Command\ClientScopeCommand::fetchClient() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
        $client = $this->fetchClient($output, /** @scrutinizer ignore-type */ $clientId);
Loading history...
163
        if (!$client instanceof Client) {
0 ignored issues
show
introduced by
$client is always a sub-type of OAuth\Client.
Loading history...
164
            $output->writeln('No client found');
165
            return;
166
        }
167
168
        $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId);
0 ignored issues
show
Bug introduced by
It seems like $scopeId can also be of type string[]; however, parameter $identifier of OAuth\Repository\ScopeRe...opeEntityByIdentifier() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
        $scope = $this->scopeRepository->getScopeEntityByIdentifier(/** @scrutinizer ignore-type */ $scopeId);
Loading history...
169
        if (!$scope instanceof Scope) {
0 ignored issues
show
introduced by
$scope is always a sub-type of OAuth\Scope.
Loading history...
170
            $output->writeln('No scope found.');
171
            return;
172
        }
173
174
        $output->writeln('Adding '. $scopeId . ' scope to ' . $client->getName() . '...');
0 ignored issues
show
Bug introduced by
Are you sure $scopeId of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
        $output->writeln('Adding '. /** @scrutinizer ignore-type */ $scopeId . ' scope to ' . $client->getName() . '...');
Loading history...
175
        $client->getScopes()->add($scope);
176
        $this->clientService->getClientRepository()->save($client);
177
        $output->writeln($scopeId . ' scope added.');
178
179
    }
180
181
    /**
182
     * @param InputInterface $input
183
     * @param OutputInterface $output
184
     * @throws Exception
185
     */
186
    private function removeScope(InputInterface $input, OutputInterface $output)
187
    {
188
        $clientId = $input->getArgument('client');
189
        $scopeId = $this->getArgOrGetUpset($input, 'scope');
190
191
        $client = $this->fetchClient($output, $clientId);
0 ignored issues
show
Bug introduced by
It seems like $clientId can also be of type null and string[]; however, parameter $id of OAuth\Command\ClientScopeCommand::fetchClient() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

191
        $client = $this->fetchClient($output, /** @scrutinizer ignore-type */ $clientId);
Loading history...
192
        if (!$client instanceof Client) {
0 ignored issues
show
introduced by
$client is always a sub-type of OAuth\Client.
Loading history...
193
            $output->writeln('No client found');
194
            return;
195
        }
196
197
        $scopes = $client->getScopes();
198
        /** @var Scope $scope */
199
        foreach ($scopes as $key => $scope) {
200
            if ($scope->getIdentifier() == $scopeId) {
201
                $scopes->remove($key);
202
                break;
203
            }
204
        }
205
206
        $this->clientService->getClientRepository()->save($client);
207
        $output->writeln($scopeId . ' scope removed.');
208
    }
209
}