Completed
Pull Request — master (#5)
by Valentyn
02:55
created

FosoCleanCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 0
loc 66
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 0 13 1
A execute() 0 18 4
1
<?php // https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/issues/544
2
3
namespace App\Command;
4
5
use FOS\OAuthServerBundle\Model\AccessTokenManagerInterface;
6
use FOS\OAuthServerBundle\Model\AuthCodeManagerInterface;
7
use FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface;
8
use FOS\OAuthServerBundle\Model\TokenManagerInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class FosoCleanCommand extends Command
14
{
15
    /**
16
     * @var AccessTokenManagerInterface
17
     */
18
    private $accessTokenManager;
19
20
    /**
21
     * @var RefreshTokenManagerInterface
22
     */
23
    private $refreshTokenManager;
24
25
    /**
26
     * @var AuthCodeManagerInterface
27
     */
28
    private $authCodeManager;
29
30
    /**
31
     * FosoCleanCommand constructor.
32
     *
33
     * @param AccessTokenManagerInterface $accessTokenManager
34
     * @param RefreshTokenManagerInterface $refreshTokenManager
35
     * @param AuthCodeManagerInterface $authCodeManager
36
     */
37
    public function __construct(AccessTokenManagerInterface $accessTokenManager, RefreshTokenManagerInterface $refreshTokenManager, AuthCodeManagerInterface $authCodeManager)
38
    {
39
        parent::__construct();
40
41
        $this->accessTokenManager = $accessTokenManager;
42
        $this->refreshTokenManager = $refreshTokenManager;
43
        $this->authCodeManager = $authCodeManager;
44
    }
45
46
    protected function configure()
47
    {
48
        $this
49
            ->setName('oauth:clean')
50
            ->setDescription('Clean expired tokens')
51
            ->setHelp(<<<EOT
52
The <info>%command.name%</info> command will remove expired OAuth2 tokens.
53
54
  <info>php %command.full_name%</info>
55
EOT
56
            )
57
        ;
58
    }
59
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $services = [
63
            'Access token' => $this->accessTokenManager,
64
            'Refresh token' => $this->refreshTokenManager,
65
            'Auth code' => $this->authCodeManager,
66
        ];
67
68
        /** @var TokenManagerInterface $service */
69
        foreach ($services as $name => $service) {
70
            if ($service instanceof TokenManagerInterface || $service instanceof AuthCodeManagerInterface) {
71
                $result = $service->deleteExpired();
72
                $output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, $name));
73
            }
74
        }
75
76
        return 0;
77
    }
78
}