restoreDefaultUserProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
18
    private Configuration $configuration;
19
20
    private UserProviderInterface $provider;
21
22
    public function __construct(ConsoleUserProvider $consoleUserProvider, Configuration $configuration, UserProviderInterface $provider)
23
    {
24
        $this->consoleUserProvider = $consoleUserProvider;
25
        $this->configuration = $configuration;
26
        $this->provider = $provider;
27
    }
28
29
    public static function getSubscribedEvents(): array
30
    {
31
        return [
32
            ConsoleEvents::COMMAND => 'registerConsoleUserProvider',
33
            ConsoleEvents::TERMINATE => 'restoreDefaultUserProvider',
34
        ];
35
    }
36
37
    public function registerConsoleUserProvider(ConsoleCommandEvent $commandEvent): void
38
    {
39
        $command = $commandEvent->getCommand();
40
        $this->consoleUserProvider->setCurrentCommand($command);
41
        $this->configuration->setUserProvider($this->consoleUserProvider);
42
    }
43
44
    public function restoreDefaultUserProvider(): void
45
    {
46
        $this->consoleUserProvider->setCurrentCommand(null);
47
        $this->configuration->setUserProvider($this->provider);
48
    }
49
}
50