Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

RefreshTokensExpireCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 49
ccs 0
cts 23
cp 0
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 15 3
A __construct() 0 5 1
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
    public function __construct(RefreshTokenStorageInterface $storage, UserRepository $repository)
34
    {
35
        parent::__construct('silverback:api-components:refresh-tokens:expire');
36
        $this->storage = $storage;
37
        $this->repository = $repository;
38
    }
39
40
    protected function configure(): void
41
    {
42
        $this
43
            ->setDescription('Expire all refresh-tokens or by user.')
44
            ->setDefinition(
45
                [
46
                    new InputArgument('username', InputArgument::OPTIONAL, 'The username'),
47
                    new InputOption('field', null, InputOption::VALUE_REQUIRED, 'The user field (username, email)', 'username'),
48
                ]
49
            )
50
            ->setHelp(
51
                <<<EOT
52
                    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
    }
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