Passed
Pull Request — master (#309)
by Sander
04:30 queued 01:41
created

ConsoleEventSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 5 1
A __construct() 0 5 1
A registerConsoleUserProvider() 0 5 1
A restoreDefaultUserProvider() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\AuditorBundle\Event;
6
7
use DH\Auditor\Configuration;
8
use DH\Auditor\User\UserProviderInterface;
9
use DH\AuditorBundle\User\ConsoleUserProvider;
10
use Symfony\Component\Console\ConsoleEvents;
11
use Symfony\Component\Console\Event\ConsoleCommandEvent;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class ConsoleEventSubscriber implements EventSubscriberInterface
15
{
16
    private ConsoleUserProvider $consoleUserProvider;
17
    private Configuration $configuration;
18
    private UserProviderInterface $provider;
19
20
    public function __construct(ConsoleUserProvider $consoleUserProvider, Configuration $configuration, UserProviderInterface $provider)
21
    {
22
        $this->consoleUserProvider = $consoleUserProvider;
23
        $this->configuration = $configuration;
24
        $this->provider = $provider;
25
    }
26
27
    public static function getSubscribedEvents(): array
28
    {
29
        return [
30
            ConsoleEvents::COMMAND => 'registerConsoleUserProvider',
31
            ConsoleEvents::TERMINATE => 'restoreDefaultUserProvider',
32
        ];
33
    }
34
35
    public function registerConsoleUserProvider(ConsoleCommandEvent $commandEvent): void
36
    {
37
        $command = $commandEvent->getCommand();
38
        $this->consoleUserProvider->setCurrentCommand($command);
39
        $this->configuration->setUserProvider($this->consoleUserProvider);
40
    }
41
42
    public function restoreDefaultUserProvider(): void
43
    {
44
        $this->consoleUserProvider->setCurrentCommand(null);
45
        $this->configuration->setUserProvider($this->provider);
46
    }
47
}
48