Passed
Push — master ( 017734...6e92fd )
by Daniel
06:25
created

RefreshTokensExpireCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 1
rs 9.9666
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Command;
15
16
use Doctrine\ORM\EntityNotFoundException;
17
use Silverback\ApiComponentsBundle\RefreshToken\Storage\RefreshTokenStorageInterface;
18
use Silverback\ApiComponentsBundle\Repository\User\UserRepository;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * @author Vincent Chalamon <[email protected]>
27
 */
28
final class RefreshTokensExpireCommand extends Command
29
{
30
    private RefreshTokenStorageInterface $storage;
31
    private UserRepository $repository;
32
33 7
    public function __construct(RefreshTokenStorageInterface $storage, UserRepository $repository)
34
    {
35 7
        parent::__construct('silverback:api-components:refresh-tokens:expire');
36 7
        $this->storage = $storage;
37 7
        $this->repository = $repository;
38 7
    }
39
40 7
    protected function configure(): void
41
    {
42
        $this
43 7
            ->setDescription('Expire all refresh-tokens or by user.')
44 7
            ->setDefinition(
45
                [
46 7
                    new InputArgument('username', InputArgument::OPTIONAL, 'The username'),
47 7
                    new InputOption('field', null, InputOption::VALUE_REQUIRED, 'The user field (username, email)', 'username'),
48
                ]
49
            )
50 7
            ->setHelp(
51
                <<<EOT
52 7
                    The <info>silverback:api-components:refresh-token:expire</info> command expires all refresh-tokens or by user:
53
                      <info>php %command.full_name%</info>
54
                      <info>php %command.full_name% username</info>
55
                    EOT
56
            );
57 7
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function execute(InputInterface $input, OutputInterface $output): int
63
    {
64
        if ($username = (string) $input->getArgument('username')) {
65
            $user = $this->repository->findOneBy([$input->getOption('field') => $username]);
66
            if (!$user) {
67
                throw new EntityNotFoundException(sprintf('User with username "%s" not found.', $username));
68
            }
69
            $this->storage->expireAll($user);
70
            $output->writeln(sprintf('RefreshTokens for user <comment>%s</comment> successfully expired.', $username));
71
        } else {
72
            $this->storage->expireAll(null);
73
            $output->writeln('RefreshTokens for all users successfully expired.');
74
        }
75
76
        return 0;
77
    }
78
}
79