Issues (128)

src/Command/RefreshTokensExpireCommand.php (1 issue)

Labels
Severity
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\UserRepositoryInterface;
19
use Symfony\Component\Console\Attribute\AsCommand;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
/**
27
 * @author Vincent Chalamon <[email protected]>
28
 */
29
#[AsCommand(name: 'silverback:api-components:refresh-tokens:expire')]
30
final class RefreshTokensExpireCommand extends Command
31
{
32
    private RefreshTokenStorageInterface $storage;
33
    private UserRepositoryInterface $repository;
34
35
    public function __construct(RefreshTokenStorageInterface $storage, UserRepositoryInterface $repository)
36
    {
37
        parent::__construct();
38
        $this->storage = $storage;
39
        $this->repository = $repository;
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this
45
            ->setDescription('Expire all refresh-tokens or by user.')
46
            ->setDefinition(
47
                [
48
                    new InputArgument('username', InputArgument::OPTIONAL, 'The username'),
49
                    new InputOption('field', null, InputOption::VALUE_REQUIRED, 'The user field (username, email)', 'username'),
50
                ]
51
            )
52
            ->setHelp(
53
                <<<EOT
54
                    The <info>silverback:api-components:refresh-token:expire</info> command expires all refresh-tokens or by user:
55
                      <info>php %command.full_name%</info>
56
                      <info>php %command.full_name% username</info>
57
                    EOT
58
            );
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output): int
65
    {
66
        if ($username = (string) $input->getArgument('username')) {
67
            $user = $this->repository->findOneBy([$input->getOption('field') => $username]);
0 ignored issues
show
The method findOneBy() does not exist on Silverback\ApiComponents...UserRepositoryInterface. Did you maybe mean findOneByEmail()? ( Ignorable by Annotation )

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

67
            /** @scrutinizer ignore-call */ 
68
            $user = $this->repository->findOneBy([$input->getOption('field') => $username]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
            if (!$user) {
69
                throw new EntityNotFoundException(sprintf('User with username "%s" not found.', $username));
70
            }
71
            $this->storage->expireAll($user);
72
            $output->writeln(sprintf('RefreshTokens for user <comment>%s</comment> successfully expired.', $username));
73
        } else {
74
            $this->storage->expireAll(null);
75
            $output->writeln('RefreshTokens for all users successfully expired.');
76
        }
77
78
        return 0;
79
    }
80
}
81